Help with Projectile Fire Script

I’m working on a top down shooter and this is currently my “firing” script.

var projectile:GameObject;
var thePosition:float;
function Update () {
if (Input.GetMouseButtonDown(0)) {
	var thePosition = transform.TransformPoint(Vector3.forward * 5);
	Instantiate(projectile, thePosition, projectile.transform.rotation);
	}
}

It only seems to work with single clicking mouse buttons as input (I’d like it to work for double click or at least a keystroke), and it just spawns the bullet in front of the player, whereas I’d like it to have some velocity.

Any help?

if the projectile has a rigidbody you can add some force to it like so

var projectile:GameObject; 
var thePosition:Vector3;
var shotForce:float;

function Update () 
{ 
    if (Input.GetMouseButtonDown(0)) 
    {
        thePosition = transform.TransformPoint(Vector3.forward * 5);
        var shot=Instantiate(projectile, thePosition, Quternion.identity);
        shot.rigidbody.AddForce(transform.forward*shotForce)
    } 
}

Input.GetMouseButtonDown works on the frame when the button goes down (the change of state from 0 to 1) Input.GetMouseButtonUp obviously is the invert and Input.GetMouseButton returns 1 as long as the button is held down. I guess that is the one you are looking for.

Now, you will have to regulate the flow of shot because you are about to get hundreds or at least tens of instantiation per second.

For your object, it does not move because the movement script is in your if statement, so it is never reached again. Add a rigidbody to your bullet prefab and use AddForce

var projectile:Transform; 
var thePosition:float; 
var force:float = 20;
function Update () { 
    if (Input.GetMouseButton(0)) { 
        var projectile=Instantiate(projectile, thePosition, projectile.transform.rotation); } }
        projectile.rigidbody.AddForce(Vector3.Forward*force);}

You could create a spawn point instead of thePosition and use transform.Find(“SpawnPoint”).transform.position instead.
Spawn Point is just an empty game object attached to the player and tagged “SpawnPoint”.
Not compulsory though.

Please go through this for the double-click thing – How can i Double click on a gameObject? - Questions & Answers - Unity Discussions

And apply the RigidBody to the bullets, and call AddForce() method on them.

http://[unity3d.com/support/documentation/ScriptReference/Rigidbody.AddForce.html][2]