0
\$\begingroup\$

This is a top-down view but in 3D coordinates, I would like to instantiate and fire a bullet from the player's gun. This script is on a spawner object at the end of the barrel.

void Fire () {

        float speed = 80;

        GameObject projectile = Instantiate (bullet, transform.position, transform.rotation) as GameObject;

        projectile.GetComponent<Rigidbody>().AddForce(transform.forward * speed);
        coolDown += Time.time;
    }

I also tried this but in this case the bullet always moves in a fixed direction (North) :

projectile.GetComponent<Rigidbody>().velocity = Vector3.forward * speed;

Bullet is an empty object and the actual bullet is it's child ( did that to overcome a rotation issue ), the problem is nomatter how high I turn up the speed the bullet travels very very slow. Both the parent and the bullet have Rigidbody.

\$\endgroup\$
0
\$\begingroup\$

Force = Mass * Acceleration, so in order to accelerate an object to 80 you'd have to apply a force of 80 * Mass for one second.

If you want to instantly accelerate the object to a set speed, you should pass ForceMode.VelocityChange into AddForce. That will instantly change the object's velocity to what you want.

projectile.GetComponent<Rigidbody>().AddForce(transform.forward * speed, ForceMode.VelocityChange);
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.