- Home /
The question is answered, right answer was accepted
2D Shooter - shooting forward at the current rotation
Hi! So I have a script where i rotate an enemy to face the player.Then I want the enemy to shoot a projectile at the angle the enemy is.I have tryed alot of different codes from the internet to shoot the projectile forward but instead my projectile does not move at all.
this causes the same thing: "rigidbody2D.AddForce(Vector3.forward Speed)" I also tryed rigidbody2D.AddForce(transform.forward Speed)" In both cases the projectile does not move. Any help is apreciated.Thank you
Have you tried changing
rigidbody2D.AddForce(Vector3.forward Speed)
to
rigidbody2D.AddRelativeForce(Vector3.forward Speed)
I don't know how it works with Vector3.forward. but I've used something similar with an x variable for adjusting (projectileSpeed).
Take a look at my code for a projectile shooting thing:
GameObject Clone;
Clone = (Instantiate (bulletPrefab, transform.position, transform.rotation)) as GameObject;
Clone.rigidbody2D.AddRelativeForce (new Vector2 (-bulletSpeed, 0));
If I change it to only AddForce, it only shoots in the same direction. Teh AddRelativeForce makes it so that the X-value is proportional to the rotation.
the problem is that i want it to go on the Z direction and if i just use forward it does not move at all
Answer by Stealthygolem · Nov 29, 2014 at 12:02 AM
Playing around with my code and his own variation, RedDevil accepted this collaborated code as an answer:
rigidbody2D.AddRelativeForce(new Vector3 (0,Speed));
I'm glad I could help, even though you did most of the work!
Answer by RedDevil · Nov 28, 2014 at 02:27 PM
alright i made it work by modifying your code: here is the final one:
rigidbody2D.AddRelativeForce(new Vector3 (0,Speed));
Submit that code as answer and i will accept it. Thank you