- Home /
The question is answered, right answer was accepted
(2D) Face Object in Direction of Travel
I have a turret which fires a projectile, that when it collides with a certain object (namely a shield attached to the player), it will bounce off using the unity physics material "bouncy" setting. The problem, however, is the projectile does not rotate in the direction it is traveling (it's a laser, if that helps you visualize it). I have done much research to try and find how to solve this, but much of it does not translate into 2D. Does anyone with experience in 2D know how I can solve this problem?
Answer by rufopufo · Jun 28, 2019 at 10:12 AM
Hi there,
You should make the "forward" vector of your projectil, match the direction of the movement.
That way, your projectil will look always in the direction of the movement.
You can achieve this by accessing the transform.forward property:
this.transform.forward = this.GetComponent<Rigidbody2D>().velocity;
I am assuming it has a rigidbody attached to it... If not, you will have to find the direction of movement in another way.
Hope it helps.
Surprising how simple this was. Thank you for the help. I did, however, have to use transform.right rather than forward since it is a 2d game, and the z axis does not exist.
Oh that's true, sorry about that :S
Glad I helped :)