||Topic was automatically imported from the old Question2Answer platform.
I have a problem…
I have a 2D game with guns and stuff (pokemon 2D style not top down) and i have the problem that my body dont turns in the direction my gun is shooting at.
For the Player I have a normal Kinematic body and For the bullets I have an area that turns with mouse_position (
And my Player only have animations for north, east, south and west…
Is it possible to shoot in the direction my character is looking at ( east, west, south or nord) ?
Is this what you want to try to achieve? Imgur: The magic of the Internet
If it’s the case, you can measure the angle between player and mouse to determine which way they are facing. Then set the animation accordingly
var angle_to_mouse = global_position.angle_to_point(get_global_mouse_position())
angle_to_mouse = rad2deg(angle_to_mouse)
angle_to_mouse = wrapf(angle_to_mouse, 0, 360)
if angle_to_mouse < 45 or angle_to_mouse >= 315:
animation = "left"
elif angle_to_mouse >= 45 and angle_to_mouse < 135:
animation = "up"
elif angle_to_mouse >= 135 and angle_to_mouse < 225:
animation = "right"
animation = "down"
Then for the bullet, you can check which direction the character is facing, and set the direction vector to the bullet on instancing it.
var bullet = bullet_scene.instance()
bullet.global_position = global_position
bullet.dir = Vector2.LEFT
bullet.dir = Vector2.RIGHT
bullet.dir = Vector2.DOWN
bullet.dir = Vector2.UP
And here’s the bullet script
var dir: Vector2
position += dir * 10