#get_angle_to issue

1 messages · Page 1 of 1 (latest)

small sparrow
#

When I attempt to use get_angle_to to shoot a bullet from a rotated sprite it shoots off in a different direction. How would I fix this? First image is the function that shoots the bullets. Second image is the line where this function is called. I have attempted to use global_position but the issue persisted.

pliant delta
#

Based on the code snippet, it looks like you're using get_angle_to correctly. It returns the angle difference between where the node is facing and the target position.

I haven't tested anything yet, but position and rotation are always in the current local coordinates.
As a first step, try stating shooter.addChild(bullet) before setting position, rotation, and the other stuff.

small sparrow
#

This worked fine before and I now know that the problem is due to the enemy sprite being rotated.

small sparrow
pliant delta
#

Vectors and trigonometry?
I'm not sure if there's a built-in function for that.

small sparrow
#

ok

#

Ill try a trig based solution.

pliant delta
#

I'm still confused about what the enemy's rotation has to do with get_angle_to
Are shooter and enemy in different coordinate spaces?

small sparrow
pliant delta
#

Oh. Why don't you rotate the bullet according the the angle from player to enemy?

bullet.rotation = player.global_rotation + rotationDiff```
All that depends on then, is that player and enemy are both directly underneath scene root
#

Even better: using player.get_angle_to(enemy.global_position) should make it irrelevant in which coordinate space either player or enemy are.

small sparrow
#

if I unders

#

I found a solution

#

this: rotation + get_angle_to(target.position)

pliant delta
small sparrow
#

the global poistion thing also didn't work. Also I got it to work by just adding the enemy's rotation to the get_angle_to result.

#

Is this is a problematic solution feel free to educate me.

pliant delta
#

No, no! What works works 😄
I'm just confused, because the global_position thing seems to work great in my test setup.

#

Here, Node2D and Node2D2 are both in separate positions, with different rotations, as are shooter and target within them.

small sparrow
#

Note that the script is attached to the enemy/boss sprite

#

Also the shooter is the scene node.

#

note that I may accedentally refer to a node as a sprite.

pliant delta
#

It's a misnomer, shooter in my scene is treated like "player"

small sparrow
#

The node called "parent" is the node used as the shooter and the enemy/boss is spawned in by a script when the scene is loaded.

pliant delta
small sparrow
#

target is the player

#

the script is attached to the boss.

pliant delta
#

Okay. My bad - I should have asked those questions first 😅

small sparrow
#

Thans for the help and I apologise for confusing you. I'm going to mark this as solved but feel free to keep talking if you want.