Rotate an object using animation

I want to rotate an object 90 degrees, the object is facing upwards and I want it animating to the right, I have this code in “init” :
go.animate(".", "rotation", go.PLAYBACK_ONCE_FORWARD, vmath.quat_rotation_z(-90), go.EASING_LINEAR, 4)

But when I run, it gives me weird behaviour, it acts as if it’s moving away (scaling down) and then it goes back, rotate to opposite direction, I don’t really understand what’s going on.

Maybe you should use radians?

I used radians(1.57) but there still some illusion of the object being scaled down and the back up, not sure if it’s happening or just my imagination.
Also I’m unable to get the rotation for comparison, I do this:

if go.get_rotation() >=  -1.57 then
	print("right")
end

How to get a specific axis?

go.get_rotation() returns a quaternion, containing x, y, z and w. So, to get rotation around z-axis you do:

go.get_rotation().z

Now I’m having this problem:

go.property("speed", 4)

function init(self)

    go.animate(".", "rotation", go.PLAYBACK_ONCE_FORWARD, vmath.quat_rotation_z(-0.7),  go.EASING_LINEAR, self.speed)
end

I get this error:
attempt to index global 'self' (a nil value)

Is this in a .script file? The code looks fine. Is it a large file? Could you share it?

I have this code inside a function that I call in “init”, maybe it’s a scoping problem ?

rotate = function()
	
	go.animate(".", "rotation", go.PLAYBACK_ONCE_FORWARD, vmath.quat_rotation_z(-1.57),  go.EASING_LINEAR, self.speed)
end

Could you please post the full thing? It’s unclear where the self is coming from. You either need to pass in self to the rotate function or have it available in the enclosing scope where you declare the rotate function.

go.property("speed", 4)


    local rotate

    function init(self)

       	rotate()
    end

    rotate = function()
    	
    	go.animate(".", "rotation", go.PLAYBACK_ONCE_FORWARD, vmath.quat_rotation_z(-1.57),  go.EASING_LINEAR, self.speed)
    end

Shouldn’t go.property() be global by default ?!

There is a property “euler” that you can use to express rotations in Euler angles. Simpler if you just wanna do z rotation. Animate property “euler.z”.

1 Like

Do you have “euler” on go? I thought it was only on sprites, but I could be wrong.

@Abdou23

The rotate function has no reference to self. self is passed to the init() function, but you need to pass that along to rotate() as well:

local rotate

function init(self)
	rotate(self)
end

rotate = function(self)
	go.animate(".", "rotation", go.PLAYBACK_ONCE_FORWARD, vmath.quat_rotation_z(-1.57),  go.EASING_LINEAR, self.speed)
end
1 Like