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)
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