- Home /
Is there a script out there that actully works for shooting bullets?
I am trying to make my guns fire bullets you know like guns do XD but the script that I am using dosnt work and I was wondering if anyone could help me fix it. I am using the newest version of Unity. code I have now:
var projectile : Rigidbody;
var initialSpeed = 20.0;
function Fire () {
var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, transform.rotation);
instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (0, 0, initialSpeed));
Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider); }
function Update () { if (Input.GetButtonDown("Fire1")) { Fire(); } }
can someone please help solve this problem soon thank you for you help.
What is it that doesn't work? Could you please provide more information?
just checking, have actually specified the rigidbody that you are instantiating in the inspector
Shouldn't you be firing the Transform that contains the rigidbody? I'd guess that you might be firing it (do you see a new GameObject pop up?) but it has no mesh, so you don't see anything.
The Bullet fires only one way if i turn left it fires right if I turn back it fires straight
Answer by Tracey P · Mar 13, 2011 at 01:53 AM
Ok the script works now its the one I posted but now can someone help me make the bullets disappear after a time and show me how to attach a particle effect to it that folows the bullet along its path. Right now my attached particle system shows up but doesn't stay with the bullet as it runs its course it just stays in the same spot it spawned. All help is appreciated thank you.
var lifeTime = 2.0;
function Awake ()
Destroy(gameObject,lifeTime);
This will detroy the object the script is attatched to in 2 seconds.
Thanks do you have a solution to the particle problem too?
Answer by efge · Mar 12, 2011 at 04:23 PM
Not shure why you use TransformDirection, but you could try this (or maybe better use Rigidbody.AddForce):
instantiatedProjectile.velocity = Vector3 (0, 0, initialSpeed);
TransformDirection is correct. What you have fires the bullet North at all times. TransformDirection twists (0,0,initspeed)
around to the direction the player is facing.
Your answer
Follow this Question
Related Questions
I need help with a firing script 1 Answer
Need help... Start Button no longer working!!!!! 0 Answers
ammo display javascript 1 Answer
How to let my gun shoot ?? 0 Answers
Gun Firing help? 2 Answers