x


Shooting. Bullet floats and sprays

Hi. I know this might sound silly... I've read the FPS tutorial and all sorts of tutorials... But when I shoot, the bullet stays at the spawn point of the gun. And you shoot millions of them as long as you keep holding on the mouse button. Here's my script:

public var bulletPrefab : Transform;
public var bulletSpeed : float = 6000;

function Update(){
    if(Input.GetButton("Fire1"))
        {
            if(!bulletPrefab || !bulletSpeed)
            {
                Debug.Log("[Shoot] 'bulletPrefab' or 'bulletSpeed' is undefined");
            }else{
                var bulletCreate = Instantiate(bulletPrefab, GameObject.Find("SpawnPoint").transform.position, Quaternion.identity);
                bulletCreate.rigidbody.AddForce(transform.forward * bulletSpeed);
            }
        }
}

Please help!

more ▼

asked Nov 30 '10 at 06:11 AM

Antonios gravatar image

Antonios
9 8 8 13

at the moment when you hold the mouse button it instantiates 1 bellet every 0 seconds thats why you get thousands, you try instead of 'if(Input.GetButton("Fire1"))' 'if(Input.GetButtonDown("Fire1"))' this means you click every time to fire a bullet but is you want to hold down to fire quickly (but not so quickly) you could try using a Coroutine to run the script every 0.1 seconds.

Nov 30 '10 at 06:38 AM Scribe
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Hi,

It should be

Input.GetButtonDown("Fire1")

Also does your bullet prefab have a rigidbody attached? Maybe thats why it doesn't move?

more ▼

answered Nov 30 '10 at 07:59 AM

TheokieZA gravatar image

TheokieZA
103 5 7 14

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x446
x329
x308
x182
x3

asked: Nov 30 '10 at 06:11 AM

Seen: 1067 times

Last Updated: Nov 30 '10 at 06:11 AM