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
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.
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
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