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. Also tried putting the script on the player itself but also didn't work.

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

 projectile.GetComponent().AddForce(transform.forward * speed);

The problem is the bullets doesn't behave as intended, instead they don't appear relative to the player rotation and they just go in a very different direction. Shouldn't "Transform.Forward" mean forward in the Z position regarding the object's transform ?

  • \$\begingroup\$ Needs more debugging. Does the bullet work if you fire it from some location independent of the player? If you log all bullet collisions, what do you get? \$\endgroup\$ – Anko Apr 24 '15 at 13:23

Maybe you spawn it too close/inside other colliders. Try spawning it at some distance.

  • \$\begingroup\$ Yes, after a close look the bullet was being instantiated a little bit inside the spawner, had to make an offset for it. Thank you. \$\endgroup\$ – Abdou023 Apr 24 '15 at 19:54

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.