# if mouse button is pressed continual add force from min. to max.and then shoot a ball

Hallo is there anybody who will help me with this code???

``````var cannonball : Transform;
var power = 0;
var speed = 5;
var powermax = 5000;
var powermin = 100;

var timeToMove : float = 0.5;       // time to move (in seconds)
private var lerpAmt : float = 0.0;  // current lerp 't' amount (private, 0-1)

if ( Input.GetButtonUp("Fire1")         // if pin is pressed
&& lerpAmt < 1 )                 // and lerpAmt is not already at max

{
lerpAmt += Time.deltaTime / timeToMove;
power = Mathf.Lerp(powermin, powermax, lerpAmt);
}

function Update() {
if(Input.GetButton("Fire1")) {
var projectile = Instantiate(cannonball,
transform.position,
transform.rotation);
Physics.IgnoreCollision(projectile.collider, collider);
}

}

``````
``````if ( Input.GetButtonUp("Fire1")         // if pin is pressed
&& lerpAmt < 1 )                 // and lerpAmt is not already at max

{
lerpAmt += Time.deltaTime / timeToMove;
power = Mathf.Lerp(powermin, powermax, lerpAmt);
}

``````

That isn't in a function, it needs to be so.

------------EDIT------------------

``````var cannonball : Transform;
var power = 0;
var speed = 5;
var powermax = 5000;
var powermin = 100;
var timeToMove : float = 0.5;       // time to move (in seconds)
private var lerpAmt : float = 0.0;  // current lerp 't' amount (private, 0-1)

function Update()
{

if ( Input.GetButtonUp ( "Fire1" ) && lerpAmt < 1 )                // and lerpAmt is not already at max
{
lerpAmt += Time.deltaTime / timeToMove;
power = Mathf.Lerp ( powermin, powermax, lerpAmt );
}

if ( Input.GetButton ( "Fire1" ) )
{
var projectile = Instantiate ( cannonball, transform.position, transform.rotation );
projectile.rigidbody.AddForce ( transform.right * power );//cannon's x axis
Physics.IgnoreCollision ( projectile.collider, collider );
}

}

``````

That's it cleaned up and in the Update function...

Try this script. I tested it and it seems to work just fine. If you don't want to automatically shoot when powermax is reached, uncheck Auto Throw in inspector. Easy.

``````var cannonball : Transform;
var speed      : float = 500; // speed is power per second
var powermax   : float = 5000;
var powermin   : float = 100;
var autoThrow  : boolean = true;

function Update()
{
if (Input.GetButtonDown("Fire1"))
StartCoroutine("Cook");
}

// This is a co-routine.
// I used the term "to cook a grenade" as popular in many action games.
function Cook()
{
var power : float = powermin;

while (Input.GetButton("Fire1"))
{
var move = speed * Time.deltaTime;
power = Mathf.MoveTowards(power, powermax, move);

if (autoThrow && power == powermax)
break;
else
yield;
}

Shoot(power);
}

function Shoot(power : float)
{
var projectile = Instantiate(cannonball,
transform.position,
transform.rotation);