Hi
i'm a bit of noob on unity and struggling a bit with my game at the moment due pretty much zero coding experience.
Basically what i want is to make a game object be able to increase its potential velocity (in my case its a sphere). I need it to charge up it's possible speed when i click down the left mouse button, and release its momentum when the button is released, gradually decreasing its speed as it moves forward. The release speed will increase depending on the length of time the button is depressed. Just so you know this is happening in a 3d environment and everything else is being controlled through the mouse i.e camera view, direction sphere is travelling ect.
As i said really haven't got much coding experience so if any could point me in the right direction with a bit of script it would be really appreciated.
Moved update progress to main question from an answer
hi,
Thanks for the link its been a big big help. i have how ever run in to abit of a road block again. It works perfectly on the first charge up but but won't reach its maximum charge on any atempts afterwards. If you could have a look at my script and give me some advice on how to fix it that would be great. Anyways here's what i managed to cobble together :
//create a private variable (not shown in inspector) to store the current set power
private var thePower : float;
//create a boolean flag we can use to stop and start the addition to power
private var increasing : boolean = false;
//create a boolean flag we can use to stop the player shooting during the shot
private var shooting : boolean = false;
//Default, the charge will go up 1 per second
var chargeSpeed : float = 1;
//set the maximum charge to be used for the maths function below
var fullcharge : float = 10;
// create a number to multiply the force by as the value of up to 256 may not be enough to
// effectively shoot a ball forward
var shotForce : float = 5;
var target: Transform;
function Update () {
//if we are not currently shooting and Jump key is pressed down
if(!shooting && Input.GetButtonDown("Jump")){
increasing=true;
}
// detect if Jump key is released and then call the Shoot function, passing current
// value of 'thePower' variable into its 'power' argument
if(!shooting && Input.GetButtonUp("Jump")){
//reset increasing to stop charge of the power bar
increasing = false;
//call the custom function below with current value of thePower fed to its argument
Shoot(thePower);
}
if(increasing){
//add to thePower variable using Time.deltaTime multiplied by barSpeed
thePower += Time.deltaTime * chargeSpeed;
//stop (or 'fight') thePower from exceeding fullWidth using Clamp
thePower = Mathf.Clamp(thePower, 0, fullcharge);
}
}
// start the 'Shoot' custom function, and establish a
// float argument to recieve 'thePower' when function is called
function Shoot(power : float){
//stop shooting occuring whilst currently shooting with this boolean flag
shooting = true;
//find the forward direction of the object assigned to the spawnPos variable
var fwd : Vector3 = transform.forward;
target.rigidbody.AddForce(fwd * power * shotForce);
yield;
shooting = false;
}