I have been trying to figure out a neat way of animating the transitional change of position for my character, but got stuck in trying to make the character turn towards the position… either I hit a gimbal-lock using “rotation” or a faulty quaternions using “rotation.z” resulting in a scaled object. (I can add gifs showing the visual issues later on request).
My last resort is to animate a different property representing the z rotation and setting this every update based on the self.crap value, but that’s an ugly hack so I would prefer not to. Appreciate any possible hint at a better solution
go.property("move_speed", 10)
go.property("turn_speed", 0.1)
function on_message(self, message_id, message, sender)
if message_id == hash("move_to") then
local direction = go.get_world_position() - message.position
-- Rotate to face target position
local forward = vmath.normalize(direction)
local rotation_z = math.atan2(forward.y, forward.x)
local target_rotation = vmath.quat_rotation_z(rotation_z)
go.animate(".", "rotation", go.PLAYBACK_ONCE_FORWARD, target_rotation, go.EASING_OUTCUBIC, self.turn_speed, 0)
go.cancel_animations(".", "position")
-- Move to target position
local distance = vmath.length(direction)
local duration = distance / self.move_speed
go.animate(".", "position", go.PLAYBACK_ONCE_FORWARD, message.position, go.EASING_LINEAR, duration, self.turn_speed, function()
msg.post(".", "stop")
end)
end
end
Have a good evening folks!