- Home /
Getting Bullets to fire in Direction of Gun Barrel
I am making a game which involves two turrets which rotate 360 degrees and fire when I press the space bar. I am stuck with making the bullets actually fire in the direction the turret is facing. I am trying to get the rotation from the parent object of the turret because that part that actually swivels. When I am running the game and I rotate the turrets using the keyboard it is the pivot (the parent object of the turret) y-axis which changes. I need to find a way to get the rotation of the y-axis and have the bullet fire in that direction. The way I am currently trying to get the rotation is leaving me with numbers which are barely above zero (might as well be zero) even though I can see in the inspector that the y value is changing so obviously I am grabbing the wrong value.
using UnityEngine;
using System.Collections;
public class TurretScript : MonoBehaviour {
public Rigidbody2D Bullet;
public float bulletSpeed = .01f;
private float turretRotationSpeed = 5f;
void Update () {
if(Input.GetKey(KeyCode.S) && gameObject.tag == "T1")
counterClockwise();
if(Input.GetKey(KeyCode.J) && gameObject.tag == "T2")
counterClockwise();
if (Input.GetKey (KeyCode.F) && gameObject.tag == "T1")
clockwise ();
if (Input.GetKey (KeyCode.L) && gameObject.tag == "T2")
clockwise ();
if (Input.GetKey (KeyCode.Space)) {
fire ();
}
}
fire(){
Rigidbody2D newBullet = (Rigidbody2D) Instantiate(Bullet, transform.GetChild(0).position, transform.rotation);
float xDir = transform.parent.rotation.x;
float yDir = transform.parent.rotation.y;
Debug.Log (xDir + " " + yDir);
Vector2 force = new Vector2 (xDir * bulletSpeed, yDir * bulletSpeed);
newBullet.AddForce(force);}
}
Answer by robertbu · May 06, 2014 at 07:59 AM
Yep, the way you are doing it will give you values in the range of -1 to 1. Transform.rotation is a Quaternion...a 4D, non-intuitive class. It's x,y,z,w components are not angles. You can use Transform.eulerAngles to get angles, but be warned that their representation is not always what you expect.
Unless your bullet is a sphere, there are two kinds of issues here: the rotation of the bullet, and the direction to fire the bullet. If you've constructed your bullet so that the 'forward' of the bullet is facing positive 'z' when the rotation is (0,0,0), then you can change your Instantiate on line 23 to:
Rigidbody2D newBullet = Instantiate(Bullet, transform.GetChild(0).position, transform.parent.rotation) as Rigidbody;
Then get rid of lines 23 - 27, and do your AddForce() like:
newBullet.AddForce(transform.parent.forward * bulletSpeed);
or if you've rotated the bullet to face the correct direction, a more typical solution would be:
newBullet.AddForce(newBullet.transform.forward * bulletSpeed);
Note that AddForce() does set any speed, and if you left the mass at the default 1.0, it takes a pretty good jolt of force to get something moving. You want to set bulletSpeed to 500 in the inspector to start.
Final note, your code hints at a problem we see on this list: the projectile hitting part of the tank when it is fired. If you have that problem, see Physics.IgnoreCollision().
Your answer
![](https://koobas.hobune.stream/wayback/20220613144332im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Getting Bullets to fire in Direction of Gun Barrel 1 Answer
Trying to make a bullet fire at the mouse using transform.up 0 Answers
Clamped Turret Doesn't Want to Lerp the Other Way 2 Answers
How to make a turret follow the center of the screen when it's rotate ? 2 Answers
Flip over an object (smooth transition) 3 Answers