Rigidbody.Velocity On Bullet, Strange Issue [Problem Solved]

Hi, I got a very weird issue that makes no sense to me I’ve tried to fix it myself but i honestly cant figure out why this is happening so i turn to you mighty power of unity forums. Anyway… Here’s the issue i got this code and it works perfectly when the player/character is moving, but if i try and shoot while the player is stationary nothing happens? (bullet does not move)

void PlayerShoot() {
GameObject bullet = Instantiate(projectile, bulletSpawn.position, bulletSpawn.rotation);
bullet.GetComponent<Rigidbody>().velocity = bullet.transform.forward * 30;
Destroy(bullet, 2f);
}

How is this method being called in your script? Can you provide that code?

Sure its very easy

if (Input.GetButtonDown("Fire1"))  {
PlayerShoot();
}

I’m guessing you have the above code in an Update method. I just tested and I have it working fine on my end. I instead used a 3rd-person transform for the bullet’s instantiated position and rotation.

private void Update() {
        if (Input.GetKey (KeyCode.Space)) {
            PlayerShoot ();
        }
    }
private void PlayerShoot() {
        print ("shoot");
        GameObject bullet = Instantiate(_bullet, transform.position, transform.rotation);
        bullet.GetComponent<Rigidbody>().velocity = bullet.transform.forward * 30;
        Destroy(bullet, 2f);
    }

Problem fixed, It was another script that i used to detect if the bullet hit something that deleted the bullet because it would sometimes collide with the gun

1 Like