# Rotating a Game Object

**URL:** <https://forum.defold.com/t/rotating-a-game-object/79419>\
**Category:** Questions\
**Created:** [January 13, 2025, 1:33pm UTC](https://forum.defold.com/t/rotating-a-game-object/79419 "2025-01-13T13:33:59Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Blue\_Wolf\_Games](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/blue_wolf_games/32/38505_2.png) [@Blue\_Wolf\_Games](https://forum.defold.com/u/Blue_Wolf_Games)\
**Post date:** [January 13, 2025, 1:33pm UTC](https://forum.defold.com/t/rotating-a-game-object/79419/1 "2025-01-13T13:33:59Z")

</div>

I’m trying to rotate the sword 90 degrees like a swinging motion, so it has to be smooth, but whenever I add code besides the original movement code, it breaks all the code for it. There are no consol or build errors when I run the game, and the other game object work fine. By the way "swing refers to left click.

 ![Screenshot 2025-01-13 065619](https://forum-defold-uploads.b-cdn.net/original/3X/2/f/2f2a68073bc8b9a568da95e1b79f72dd567bbe33.png)  
 ![Screenshot 2025-01-13 065556](https://forum-defold-uploads.b-cdn.net/original/3X/7/f/7f4c1091ad458eadb6b953cce401836f315b6276.png)

---

<div class="post-metadata">

**Author:** ![Halfstar](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/halfstar/32/26587_2.png) [@Halfstar](https://forum.defold.com/u/Halfstar)\
**Post date:** [January 13, 2025, 3:17pm UTC](https://forum.defold.com/t/rotating-a-game-object/79419/2 "2025-01-13T15:17:12Z")

</div>

First, you are using `go.get_rotation`, which is used to read the current rotation of the object, but not change it. You have to use `go.set_rotation` to set the rotation.

Second, `go.set_rotation` will need a quaternion as argument, not three numbers in degrees.

Third, you most likely want to rotate against the z axis, not y axis. In your screenshot, the red line is the x axis, the green line the y axis, and the z axis is from your point of view, which you want to rotate around.

So you have to use this for an instant 90 degrees rotation:

```lua
go.set_rotation(vmath.quat_rotation_z(90 * math.pi / 180))

```

Or use `go.animate` for a smooth rotation:

```lua
local duration = 1.0
go.animate(".", "euler.z", go.PLAYBACK_ONCE_FORWARD, 90, go.EASING_INOUTSINE, duration)

```

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [January 13, 2025, 7:20pm UTC](https://forum.defold.com/t/rotating-a-game-object/79419/3 "2025-01-13T19:20:53Z")

</div>

> [@Halfstar](#):
>
> Or use `go.animate` for a smooth rotation

Example: [Euler Rotation](https://defold.com/examples/animation/euler_rotation/)

---

<div class="post-metadata">

**Author:** ![Blue\_Wolf\_Games](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/blue_wolf_games/32/38505_2.png) [@Blue\_Wolf\_Games](https://forum.defold.com/u/Blue_Wolf_Games)\
**Post date:** [January 13, 2025, 9:45pm UTC](https://forum.defold.com/t/rotating-a-game-object/79419/4 "2025-01-13T21:45:07Z")

</div>

Is this right?

 ![Screenshot 2025-01-13 154401](https://forum-defold-uploads.b-cdn.net/original/3X/a/4/a4a6283ee7048f0f5ba2cf3aa1006f075c7fe0df.png)

---

<div class="post-metadata">

**Author:** ![Halfstar](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/halfstar/32/26587_2.png) [@Halfstar](https://forum.defold.com/u/Halfstar)\
**Post date:** [January 13, 2025, 9:52pm UTC](https://forum.defold.com/t/rotating-a-game-object/79419/5 "2025-01-13T21:52:48Z")

</div>

If “Sword” is the correct address from that script, it should work to rotate the object once.

---

<div class="post-metadata">

**Author:** ![Blue\_Wolf\_Games](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/blue_wolf_games/32/38505_2.png) [@Blue\_Wolf\_Games](https://forum.defold.com/u/Blue_Wolf_Games)\
**Post date:** [January 13, 2025, 10:13pm UTC](https://forum.defold.com/t/rotating-a-game-object/79419/6 "2025-01-13T22:13:24Z")

</div>

It doesn’t work. “Sword” is the game object. Also, the movement script still doesn’t work (not the rotate the normal movement) unless I remove the rotate script.

---

<div class="post-metadata">

**Author:** ![Halfstar](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/halfstar/32/26587_2.png) [@Halfstar](https://forum.defold.com/u/Halfstar)\
**Post date:** [January 13, 2025, 11:02pm UTC](https://forum.defold.com/t/rotating-a-game-object/79419/7 "2025-01-13T23:02:37Z")

</div>

Make sure that you pick the correct address of the game object (see [Addressing in Defold](https://defold.com/manuals/addressing/)).

And is the line of code even called, for example if you do `print("Test")` there? Or else the input focus or action id might be wrong.

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [January 14, 2025, 6:38am UTC](https://forum.defold.com/t/rotating-a-game-object/79419/8 "2025-01-14T06:38:37Z")

</div>

> [@Blue\_Wolf\_Games](#):
>
> ![Screenshot 2025-01-13 154401](https://forum-defold-uploads.b-cdn.net/original/3X/a/4/a4a6283ee7048f0f5ba2cf3aa1006f075c7fe0df.png)

Please don’t include code as a screen shot. Include it as text instead!

You probably only want to trigger the attack animation on the first frame when the key is pressed?

```lua
if action_id == hash("Swing") and action.pressed then
    go.animate ...
end
```

---

<div class="post-metadata">

**Author:** ![Blue\_Wolf\_Games](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/blue_wolf_games/32/38505_2.png) [@Blue\_Wolf\_Games](https://forum.defold.com/u/Blue_Wolf_Games)\
**Post date:** [January 14, 2025, 9:53pm UTC](https://forum.defold.com/t/rotating-a-game-object/79419/9 "2025-01-14T21:53:26Z")

</div>

This is the current code for the Sword:

```lua
function init(self)
	msg.post(".", "acquire_input_focus") -- <1>
	self.vel = vmath.vector3() -- <2>    
end

function update(self, dt)
	local pos = go.get_position() -- <3>
	pos = pos + self.vel * dt -- <4>
	go.set_position(pos) -- <5>

	self.vel.x = 0 -- <6>
	self.vel.y = 0
end

function on_input(self, action_id, action)
	if action_id == hash("Up") then
		self.vel.y = 300 -- <7>
	elseif action_id == hash("Down") then
		self.vel.y = -300
	elseif action_id == hash("Left") then
		self.vel.x = -300 -- <8>
	elseif action_id == hash("Right") then
		self.vel.x = 300
	end
end
function on_input(action_id, action)
	if action_id == hash("Swing") and action.pressed then
		local duration = 1.0
		go.animate("/Sword", "euler.z", go.PLAYBACK_ONCE_FORWARD, 90, go.EASING_INOUTSINE, duration)
	end
end

```

---

<div class="post-metadata">

**Author:** ![Halfstar](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/halfstar/32/26587_2.png) [@Halfstar](https://forum.defold.com/u/Halfstar)\
**Post date:** [January 14, 2025, 10:12pm UTC](https://forum.defold.com/t/rotating-a-game-object/79419/10 "2025-01-14T22:12:38Z")

</div>

You can only have one function with the same name. So one `on_input()` is ignored, as a function with that name already exists. Also your second `on_input()` forgot the `self`, which has to be the first argument, as they are defined not by name, but order.

You have to put the code in a single `on_input()`, like this:

```lua
function on_input(self, action_id, action)
	if action_id == hash("Up") then
		self.vel.y = 300
	elseif action_id == hash("Down") then
		self.vel.y = -300
	elseif action_id == hash("Left") then
		self.vel.x = -300
	elseif action_id == hash("Right") then
		self.vel.x = 300
	elseif action_id == hash("Swing") and action.pressed then
		local duration = 1.0
		go.animate("/Sword", "euler.z", go.PLAYBACK_ONCE_FORWARD, 90, go.EASING_INOUTSINE, duration)
	end
end

```

---

<div class="post-metadata">

**Author:** ![Blue\_Wolf\_Games](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/blue_wolf_games/32/38505_2.png) [@Blue\_Wolf\_Games](https://forum.defold.com/u/Blue_Wolf_Games)\
**Post date:** [January 14, 2025, 10:44pm UTC](https://forum.defold.com/t/rotating-a-game-object/79419/11 "2025-01-14T22:44:03Z")

</div>

How would I make it wait 0.1 secs and then return to the previous position, because the current code isn’t working.

| | | go.animate(/Sword, euler.z, go.PLAYBACK\_ONCE\_FORWARD, -290, go.EASING\_INOUTSINE, duration) |
| --- | --- | --- |
| | | timer.delay(0.1, false, self) |
| | | go.animate(/Sword, euler.z, go.PLAYBACK\_ONCE\_FORWARD, 290, go.EASING\_INOUTSINE, duration) |

---

<div class="post-metadata">

**Author:** ![Halfstar](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/halfstar/32/26587_2.png) [@Halfstar](https://forum.defold.com/u/Halfstar)\
**Post date:** [January 14, 2025, 10:57pm UTC](https://forum.defold.com/t/rotating-a-game-object/79419/12 "2025-01-14T22:57:50Z")

</div>

You can give a callback function to `go.animate()` that will run after it is done, like this:

```lua
function on_input(self, action_id, action)
	if action_id == hash("Up") then
		self.vel.y = 300
	elseif action_id == hash("Down") then
		self.vel.y = -300
	elseif action_id == hash("Left") then
		self.vel.x = -300
	elseif action_id == hash("Right") then
		self.vel.x = 300
	elseif action_id == hash("Swing") and action.pressed then
		local duration = 1.0
		local delay = 0.1
		go.cancel_animations("/Sword", "euler.z")
		go.set_rotation(vmath.quat_rotation_z(0), "/Sword")
		go.animate("/Sword", "euler.z", go.PLAYBACK_ONCE_FORWARD, 90, go.EASING_INOUTSINE, duration, 0, function(self, url, property)
			go.animate("/Sword", "euler.z", go.PLAYBACK_ONCE_FORWARD, 0, go.EASING_INOUTSINE, duration, delay)
		end)
	end
end

```

---

<div class="post-metadata">

**Author:** ![Blue\_Wolf\_Games](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/blue_wolf_games/32/38505_2.png) [@Blue\_Wolf\_Games](https://forum.defold.com/u/Blue_Wolf_Games)\
**Post date:** [January 14, 2025, 11:04pm UTC](https://forum.defold.com/t/rotating-a-game-object/79419/13 "2025-01-14T23:04:36Z")

</div>

Is there any way to stop people from doing this?

---

<div class="post-metadata">

**Author:** ![Halfstar](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/halfstar/32/26587_2.png) [@Halfstar](https://forum.defold.com/u/Halfstar)\
**Post date:** [January 14, 2025, 11:31pm UTC](https://forum.defold.com/t/rotating-a-game-object/79419/14 "2025-01-14T23:31:45Z")

</div>

You could set a variable (for example `attacking`) to `true` when the attack starts, and only set it to `false` when the attack is done. Then only allow the attack to start if the variable is `false`.

---

<div class="post-metadata">

**Author:** ![Blue\_Wolf\_Games](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/blue_wolf_games/32/38505_2.png) [@Blue\_Wolf\_Games](https://forum.defold.com/u/Blue_Wolf_Games)\
**Post date:** [January 15, 2025, 12:04am UTC](https://forum.defold.com/t/rotating-a-game-object/79419/15 "2025-01-15T00:04:21Z")

</div>

I did that but is there a reason why the variable changes after only the first swing?

Here’s the code:

```
	go.animate("/Sword", "euler.z", go.PLAYBACK_ONCE_FORWARD, -290, go.EASING_INOUTSINE, duration, 0, function(self, url, property)
		go.animate("/Sword", "euler.z", go.PLAYBACK_ONCE_FORWARD, 0, go.EASING_INOUTSINE, duration, delay)
		attacking = false

```

---

<div class="post-metadata">

**Author:** ![Halfstar](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/halfstar/32/26587_2.png) [@Halfstar](https://forum.defold.com/u/Halfstar)\
**Post date:** [January 15, 2025, 12:12am UTC](https://forum.defold.com/t/rotating-a-game-object/79419/16 "2025-01-15T00:12:21Z")

</div>

So you only want the player to attack again if the sword has returned to the starting angle? Then you have to set `attacking` to `false` in the callback of the second (inner) `go.animate()`, not in the callback of the first (outer).

---

<div class="post-metadata">

**Author:** ![Blue\_Wolf\_Games](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/blue_wolf_games/32/38505_2.png) [@Blue\_Wolf\_Games](https://forum.defold.com/u/Blue_Wolf_Games)\
**Post date:** [January 15, 2025, 12:44am UTC](https://forum.defold.com/t/rotating-a-game-object/79419/17 "2025-01-15T00:44:02Z")

</div>

Wait what, like in the (), where.

---

<div class="post-metadata">

**Author:** ![Halfstar](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/halfstar/32/26587_2.png) [@Halfstar](https://forum.defold.com/u/Halfstar)\
**Post date:** [January 15, 2025, 1:13am UTC](https://forum.defold.com/t/rotating-a-game-object/79419/18 "2025-01-15T01:13:22Z")

</div>

For example like this:

```lua
local function attack_done(self)
	self.attacking = false
end

function on_input(self, action_id, action)
	if action_id == hash("Up") then
		self.vel.y = 300
	elseif action_id == hash("Down") then
		self.vel.y = -300
	elseif action_id == hash("Left") then
		self.vel.x = -300
	elseif action_id == hash("Right") then
		self.vel.x = 300
	elseif action_id == hash("Swing") and action.pressed and not self.attacking then
		local duration = 1.0
		local delay = 0.1
		self.attacking = true
		go.cancel_animations("/Sword", "euler.z")
		go.set_rotation(vmath.quat_rotation_z(0), "/Sword")
		go.animate("/Sword", "euler.z", go.PLAYBACK_ONCE_FORWARD, 90, go.EASING_INOUTSINE, duration, 0, function(self, url, property)
			go.animate("/Sword", "euler.z", go.PLAYBACK_ONCE_FORWARD, 0, go.EASING_INOUTSINE, duration, delay, attack_done)
		end)
	end
end

```

---

<div class="post-metadata">

**Author:** ![Blue\_Wolf\_Games](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/blue_wolf_games/32/38505_2.png) [@Blue\_Wolf\_Games](https://forum.defold.com/u/Blue_Wolf_Games)\
**Post date:** [January 15, 2025, 2:49am UTC](https://forum.defold.com/t/rotating-a-game-object/79419/19 "2025-01-15T02:49:23Z")

</div>

It works! Thank you for helping me through all this.
