Help with rotation

Godot Version:

Godot_V.4.3

Question

I have a probelm with look_at().I have a canon and i added the script so the canon looks in the direction of the player.He is looking the player but the wrong side is looking at player.Any tips for fixing this?
Thanks

Default rotation is zero which is to the right, ie Vector2(1,0)

Is your cannon’s texture facing to the right?

Yes it is looking to the right

Can you show the relevant bits of code?

PS look_at expects global coordinates BTW. Are you using global_position for your look at point?

func _process(delta: float) - void:
$”Canon/AnimatedSprite2D”.look_at(player.global_position)

Btw what is the difference between global_position and position

Global position is the absolute coordinates in the world coordinate system. Position is the relative position to the immediate parent node. Depending on your tree structure, these could be the same thing.

Try adding this before you do the look_at.

print("Node to turn: ", $”Canon/AnimatedSprite2D”.name)
print("Orignal rotation: ", $”Canon/AnimatedSprite2D”.rotation)
print("Player global_position: ", player.global_position)
print("We want: ", (player.global_position - $”Canon/AnimatedSprite2D”.global_position).angle())

And immediately after the look at:

print("New rotation: ", $”Canon/AnimatedSprite2D”.rotation)

What do you get?

Node to turn: AnimatedSprite2D
Orignal rotation: 3.1415901184082
Player global_position: (1163, 560)
We want: 1.39989995956421
Node to turn: AnimatedSprite2D
Orignal rotation: 3.1415901184082
Player global_position: (1163, 560)
We want: 1.39989995956421
New rotation: 3.1415901184082
New rotation: 1.3999582529068
I get this

1 Like

Well it is rotating to the desired direction. I wonder why it is being called twice though? That indicates a problem somewhere to start with.

It looks like your cannon was already rotated to PI. Does your cannon sprite have a rotation applied to it to turn it to the right. If so this is overridden when you reset the rotation.

Also, why are you using $”Canon/AnimatedSprite2D”

Should you not be rotating Canon, instead of the animated sprite. I presume it is Canon that is moved.

Why is your Original Rotation equal to PI. If your canon animation needed to be rotated, when you look_at, you can just add PI. I would consider this ‘dirty’ though but might be easier than rotating all your animated sprites.

$”Canon/AnimatedSprite2D”.look_at(player.global_position)
$”Canon/AnimatedSprite2D”.rotation += PI

That actualy works.
Thanks for spending time for my problem.

1 Like