Game Object Rotation question

I’m not sure what I’m doing wrong when it comes to rotating game objects, I’m not used to having to use fancy vmath, so I don’t really know what I’m doing. :sweat_smile: I have this code here, where the “walk_down” and “walk_up” for the rotation are working…probably because the z is 0. But on the “walk_left” and “walk_right” conditions, the sprite completely dissappears. :sweat: I’ve looked at the examples that use rotation, but they all use calculations instead of manually setting so I don’t really know how to do this. Any help would be appreciated.

		local p = go.get_position("/test1_go_weapon")
		local i = go.get_id("/test1_go_weapon")
		local pw = go.get_position("/test1_go_slash")
		local iw = go.get_id("/test1_go_slash")
		local rw = go.get_rotation(iw)
		if self.current_anim == hash("anim_hero_walk_down") then
			p.x = -8
			p.y = 3
			p.z = 0.1

			pw.x = 0
			pw.y = -23
			rw.z = 0
		elseif self.current_anim == hash("anim_hero_walk_up") then
			p.z = -0.1
			p.x = 8
			p.y = 3

			pw.x = 3
			pw.y = 23
			rw.z = 0
		elseif self.current_anim == hash("anim_hero_walk_right") then
			p.z = 0.1
			p.x = 0

			pw.x = 27
			pw.y = 0
			rw.z = 180
		elseif self.current_anim == hash("anim_hero_walk_left") then
			p.z = 0
			p.x = 0
			p.y = 3

			pw.x = -25
			pw.y = 0
			rw.x = 0
			rw.y = 0
			rw.z = 270
		end
		go.set_position(p, i)
		go.set_position(pw, iw)
		go.set_rotation(rw, iw)
	end

The rotation is a quaternion.
If you don’t want to use that, use euler angles instead.
E.g. go.set("euler.x", 90)

3 Likes

You can set rotation around an axis like this:

-- angle should be in radians
local angle = math.rad(270)
-- create a quaternion with rotation around the z-axis
go.set_rotation(vmath.quat_rotation_z(angle))
6 Likes

Thanks both of you, I got it. :grinning_face_with_smiling_eyes:

1 Like