- Home /
Trouble with script to fire a bullet from a handgun
This is a java script to fire a prefab i named "9mmBullet" from a Baretta i modeled, it has an empty GameObject named "Spawnpoint" in front of it.
I am new to this so my script will be well out but i think i got the general idea, can anyone help?
var BulletSpeed = 10;
function Update () { if(Input.GetButtonDown("Fire1")){ Instantiate(Find.GameObject("9mmBullet"), Vector3 * BulletSpeed, position.transform(Find.GameObject("Spawnpoint"))); } BulletSpeed = AddForce; }
Answer by skovacs1 · Nov 10, 2010 at 05:24 PM
No kidding, this is way out.
Instantiate takes:
- An GameObject (or derived class) to instantiate. You are finding an object already in the scene called 9mmBullet and passing this. Does this object exist in the scene? If you wanted to pass a prefab of your bullet, then you should do that instead.
- A Vector3 position. Vector3 is a struct and you are multiplying this struct by a speed to get the initial position? This not only won't work, but it doesn't make sense. you probably want your spawnpoint's position to go here.
- A Quaternion representing rotation to instantiate with. This part, you are passing something so wrong that I can't even begin to describe it.
What is AddForce? A variable? Where is it? Are you trying to make the bullet move? Why would you be adding to the speed variable in this script then? This is very wrong as well.
Try looking at the documentation. There's even a script that does what you are trying to do in the example.
What you are looking for is something closer to this:
var bullet : Rigidbody; //Your bullet prefab. Should have a RigidBody var speed : float = 10.0f; var muzzlePoint : Transform; //Your SpawnPoint
function Update() { if(Input.GetButtonDown("Fire1")) { var instance : Rigidbody = Instantiate(bullet, muzzlePoint.position, muzzlePoint.rotation); instance.velocity = muzzlePoint.forward * speed; } }
Your answer
Follow this Question
Related Questions
My gun won't fire. ( Javascript ) 1 Answer
gun ammo script 2 Answers
Setting bullet instansiate direction? help? 1 Answer
Need Shooting C# script help 2 Answers
machine gun js timing 1 Answer