[Unity2D] Firing bullets based on gun orientation
So when the player clicks, it instantiates a bullet prefab as well as gives the rigidbody2D a force. The issue is I don't know what vector2 to use I've tried Vector2.right, any combination of position and rotation vectors I'm out of ideas. This is my code.
GameObject clone = (GameObject) Instantiate(bullet, muzzle.position, transform.rotation);
clone.GetComponent<Rigidbody2D>().AddForce(Vector2.right * bulletSpeed);
Answer by Eno-Khaon · May 09, 2016 at 04:18 AM
The direction they should move is dependent on the orientation of your gun sprite. For example, let's say the gun points to the right naturally. In that case, rather than Vector2.right, you can use transform.right for the relative direction as the gun rotates around.
clone.GetComponent<Rigidbody2D>().AddForce(transform.right * bulletSpeed);
Your answer
Follow this Question
Related Questions
Get Opposite of a Rotation Stored In a Quaternion 0 Answers
More problems with arrow shooting 0 Answers
How do I instantiate an objects direction and speed seperately? 1 Answer
Unet isLocalplayer issues with child objects of player 0 Answers
Addforce function not adding force in a 2D game and cannot figure out why 2 Answers