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.
#get_angle_to issue
1 messages · Page 1 of 1 (latest)
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.
This worked fine before and I now know that the problem is due to the enemy sprite being rotated.
Is there a way to get the angle to the player position without using look_at?
Vectors and trigonometry?
I'm not sure if there's a built-in function for that.
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?
The enemy is rotating the bullet according to the difference between its own rotation and the angle to the player while shooter, which is just the scene node, is not rotated at all. This also explains why the problem only cropped up when the enemy is not pointed right.
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.
Sorry, I forgot to ping you earlier.
I also noticed I was having a dumb moment in the vectors and trig comment. You'd again use the get_angle_to for that, except from enemy to player, and then rotate it (2*PI) radians
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.
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.
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.
It's a misnomer, shooter in my scene is treated like "player"
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.
But you have "parent" responsible for spawning the bullets, right? And in your very first code snippet "target" is either player or the boss, and position is the position of the respective other?
If it works, it works.
I'm just confused. Would you like to have a look at my test code snippet for the global_rotation implementation?
Okay. My bad - I should have asked those questions first 😅
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.