Rotating game object but attached sprite is not

Hello Everyone,

So I am trying to get my feet wet with defold and for that I have so far created a collection where I just want to control (rotate) the hero. I have attached an orthographic camera and a sprite (that gets a static image from an atlas component (seems that’s the simplest way to import an asset?).

As it can be seen in the screenshot there is also a script that is meant to capture input from the keyboard and rotate the hero accordingly:

local turning_speed = math.rad(90)
local turn_direction = 0 -- -1 for right, 1 for left, 0 for none
local is_rotating_left = false
local is_rotating_right = false


function init(self)
	msg.post(".", "acquire_input_focus")
	turn_direction = 0
	is_rotating_left = false
	is_rotating_right = false
end


function update(self, dt)
	if is_rotating_left == true then
		print("Turning Left!")
		turn_direction = 1
		local current_rot = go.get_rotation()
		local delta_rot = vmath.quat_rotation_z(turn_direction * turning_speed * dt)
		go.set_rotation(current_rot * delta_rot)
		sprite.set_hflip("#heroSprite", true)

		--!Debug:
		print("Cosmonauter rotation: ", go.get_rotation("cosmonauter"))
		print("Sprite rotation: ", go.get_rotation("#heroSprite"))

		is_rotating_left = false
		turn_direction = 0
	elseif is_rotating_right == true then
		print("Turning Right!")
		turn_direction = -1
		local current_rot = go.get_rotation()
		local delta_rot = vmath.quat_rotation_z(turn_direction * turning_speed * dt)
		go.set_rotation(current_rot * delta_rot)

		--!Debug:
		print("Cosmonauter rotation: ", go.get_rotation("cosmonauter"))
		print("Sprite rotation: ", go.get_rotation("#heroSprite"))

		is_rotating_right = false
		turn_direction = 0
	end
end

function on_input(self, action_id, action)
	if action_id == hash("left") then
		--turn left
		is_rotating_left = true
		is_rotating_right = false
	elseif action_id == hash("right") then
		--turn right
		is_rotating_left = false
		is_rotating_right = true
	end
end

According to print debugging when I run the game the rotation of both the gameobject and the sprite change:

DEBUG:SCRIPT: Turning Left!
DEBUG:SCRIPT: Cosmonauter rotation: vmath.quat(0, 0, 0.98753148317337, 0.1574205160141)
DEBUG:SCRIPT: Sprite rotation: vmath.quat(0, 0, 0.98753148317337, 0.1574205160141)
DEBUG:SCRIPT: Turning Left!
DEBUG:SCRIPT: Cosmonauter rotation: vmath.quat(0, 0, 0.98951542377472, 0.14442597329617)
DEBUG:SCRIPT: Sprite rotation: vmath.quat(0, 0, 0.98951542377472, 0.14442597329617)
DEBUG:SCRIPT: Turning Left!
DEBUG:SCRIPT: Cosmonauter rotation: vmath.quat(0, 0, 0.99132257699966, 0.13145007193089)
DEBUG:SCRIPT: Sprite rotation: vmath.quat(0, 0, 0.99132257699966, 0.13145007193089)

However, my problem is that the sprite just sits there without visually moving at all.

I am trying to obtain the following effect:

Cosmonauter-v0.9.1ComparedToPrototype

What am I doing wrong?

I think this is happening because your camera and sprite has the same parent. When you rotate the game object, it rotates both sprite and camera so sprite’s relative rotation stays 0. You should create another game object for the camera and make it follow the player (a short tutorial on smooth camera follow for player: Camera Follow Tutorial)

2 Likes

Thank you very much! You are correct and it makes total sense. Just by adding a new game object in the collection and moving the camera there I can now see the full rotation I was expecting.

Thank you very much for your help and assistance, much appreciated!

1 Like

Glad to be of help and good luck with Defold :slight_smile:

1 Like