Rotate GO towards the cursor with go.animate

Hello, dear Defold community! I have a question. I want to rotate GO towards the mouse in function go.animate
I’ve made some research and found the solution how to rotate GO

    local mouse_position = vmath.vector3(action.x, action.y, 0)
	local object_position = go.get_position()
	local direction = mouse_position - object_position
	local angle = vmath.quat_rotation_z(math.atan2(direction.y, direction.x))
	go.set_rotation(angle)

It works perfectly
but I don’t want to use go.set_rotation(angle)

I want to use go.animate and there are I have some problems

1 Like

This code works for animate:

function init(self)
 msg.post(".", "acquire_input_focus")
end

function on_input (self, action_id, action)
 local mouse_position = vmath.vector3(action.x, action.y, 0)
 local object_position = go.get_position()
 local direction = mouse_position - object_position
 local angle = math.atan2(direction.y, direction.x)
 angle = angle/math.pi * 180
 if action_id == hash ("touch") then
  if action.pressed then
   go.animate(".", "euler.z", go. PLAYBACK_ONCE_FORWARD, angle, go. EASING_OUTCUBIC, 1)
   go.animate(".", "position", go. PLAYBACK_ONCE_FORWARD, mouse_position, go. EASING_OUTCUBIC, 1, 0)
  end
 end
end
6 Likes

thank you!

1 Like