nerberb
December 22, 2016, 2:34am
1
I want to make a gun, so far it spawns bullets but those bullets travel forward.
this is my code
void FixedUpdate() { bulletSpawnPoint = GameObject.Find("BulletSpawnPoint").transform.position; if (Input.GetKey (KeyCode.Z)) { Instantiate (bullet); bullet.transform.position = bulletSpawnPoint; bullet.GetComponent<Rigidbody> ().AddForce (0, 300, 0); }
What should I do?
If it’s 3D, use transform.forward.
As for actually applying the force, you want to do something like this.
GameObject newBullet = (GameObject)Instantiate(bullet) as GameObject;
newBullet.transform.position = bulletSpawnPoint;
newBullet.GetComponent<Rigidbody>().AddForce(transform.forward);
Ok so the bullet prefab needs to have a script attached to it that either at start or in update adds force
Rosh7X
April 10, 2017, 1:03pm
4
No you can add force to the Bullet through the gun script, just when you instantiate the bullet you give it a name, then you can do something like this
Bullet.Getcomponent < Rigidbody >().Addforce(transform.forward)
Not at home so I don’t have an actual script to show you. Hope this helps