Bullet shooting in the wrong angle.
When trying to make a shooting script, i was getting a problem where if i moved in the world space(Using the FPSController), the way the bullet shot would be dramatically changed. I dont know why its doing this but heres my code.
public float speed = 10f;
public GameObject bulletPrefab;
public Transform bulletSpawn;
public void Update()
{
var x = Input.GetAxis ("Horizontal") * Time.deltaTime * 150.0f;
var z = Input.GetAxis ("Vertical") * Time.deltaTime * 150.0f;
transform.Rotate (0, x, 0);
transform.Rotate (0, 0, z);
if (Input.GetButtonDown("Fire1")) {
Fire ();
}
}
public void Fire() {
var bullet = Instantiate ( bulletPrefab, bulletSpawn.position, bulletSpawn.rotation);
bulletPrefab.GetComponent<Rigidbody>().useGravity = false;
// Add velocity to the bullet
bullet.GetComponent<Rigidbody>().velocity = bullet.transform.forward * speed;
// Destroy the bullet after 2 seconds
Destroy(bullet, 2.0f);
}
I have no idea why this isnt working, plz help. Thanks in advance!
Okay i found out what i did wrong, the lines
var x = Input.GetAxis ("Horizontal") * Time.deltaTime * 150.0f; var z = Input.GetAxis ("Vertical") * Time.deltaTime * 150.0f; transform.Rotate (0, x, 0); transform.Rotate (0, 0, z);
were commented out, and now it will shoot straight forward with no problems, except the bullet rotation is always 90* to the gun, ill keep you updated
Your answer
Follow this Question
Related Questions
Destroy instantiated Projectile after time 1 Answer
Easy question: X axis overriding Y. Please help! :D 0 Answers
How do I have my character aim where my mouse cursor is? 2 Answers
Score Reset on 2nd Play 1 Answer
ClientRpc not functioning 0 Answers