0 votes

I have a player that aims and shoots bullets.
The bullets are an instantiated node of type RigidBody2D.
When I instantiate the bullets, I have no trouble rotating them to the correct appearance in any direction.

When the bullets hit anything else (players, walls, and so on), they bounce properly, as per the physicsmaterialoverride.bounce property, since I set it to 1.

But when the bounce occurs, the bullets still faces the same direction. A fireball fired with a trajectory of (1,1) will still be facing that direction even after reflecting off a wall.

I would like to rotate the whole object to the correct orientation, but do not see how. I want to just get the built-in vector2.bounce() function to work, but I don't know how to get collision data from a RigidBody2D. (All sources I find assume you're using a KinematicBody2D object with moveandcollide, and not a RigidBody2D object with integrate_forces, which is what I am using.)

Halp? :)

Godot version 3.3.4 stable official
in Engine by (50 points)

1 Answer

+1 vote
Best answer

Bullets have already bounced, so You don't need bounce vector , but current linear_velocity vector. So no need to grab collision data, just use the collision moment to set up bullet facing to its velocity. You can use short yield to make sure physics engine reads correct vector right after collision

by (7,447 points)
selected by

This is exactly what I was hoping to hear. :)

I also foolishly tried to just change the sprite, and had a momentary problem.
Instead, I just rotated the whole bullet (which is what I wanted to do anyway), and it was perfect. My code, for those who might want it, was this:

self.rotation = linear_velocity.normalized().angle()
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.