Rotate go on dragging mouse horizontal over screen

How I can rotate game object on dragging horizontally?

Below clip is just using “look at” example but this was not something I wanted. Ability to drag from any point on screen horizontally will rotate object from initial angle point.

More like other game clip video shared in this post -
Animate go rotation on input in continuous

On press, store the x position of the input. On each subsequent frame calculate the delta (difference between current x and previous x), then apply rotation accordingly. Then finally store the current x, and compare it to the next one etc etc.

Something like this:

	if action.pressed then
		self.last_x = action.x
	end

	if self.last_x then
		local delta = action.x - self.last_x
		local rotation = go.get(go.get_id(), "euler.z")
		rotation = rotation + 10 * (delta/50)
		go.set(go.get_id(),"euler.z", rotation)
		self.last_x = action.x
	end

	if action.released then
		self.last_x = nil
	end
1 Like

Instead of

local delta = action.x - self.last_x
self.last_x = action.x

you should be able to use action.dx which is the delta x value (documentation)

4 Likes

That’s much neater, thanks for pointing it out!

1 Like