Question by
unity_vZjkgPrK8jOT2Q · Jun 03, 2018 at 12:27 AM ·
instantiatebulletforward
Instantiated Bullet flying in wrong direction
I am fairly new to coding and I am trying to make a game where players shoot foam darts. The script is for a full auto weapon. The bullet instantiated flies "Upward" rather than "Forward", my script is here:
if (Input.GetButton("Fire1"))
{
GameObject Temporary_Bullet_Handler;
Temporary_Bullet_Handler = Instantiate(Bullet, Bullet_Emitter.transform.position, Bullet_Emitter.transform.rotation) as GameObject;
Temporary_Bullet_Handler.transform.Rotate(Vector3.left * 90);
Rigidbody Temporary_RigidBody;
Temporary_RigidBody = Temporary_Bullet_Handler.GetComponent<Rigidbody>();
Temporary_RigidBody.AddForce(transform.forward * Bullet_Forward_Force);
Destroy(Temporary_Bullet_Handler, 3.0f);
}
The bullet flies up regardless of the Bullet's spawning point rotation.
Comment
Answer by Aquatron17 · Jun 06, 2018 at 01:13 AM
It could be because the GameObject's Z transform component is flipped. Try transform.down * Bullet_Forward_Force. let me know if it works
Answer by Bravil · Jun 06, 2018 at 03:49 AM
Hi, the problem is here:
Temporary_Bullet_Handler.transform.Rotate(Vector3.left * 90);
You are rotating your bullet on its X on -90 degrees making its Z look up.