- Home /
Charging up the speed of an game object through a mouse click
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;
}
Answer by Alexandre Zakime · Mar 23, 2011 at 05:09 PM
Hi!
you may want to look this: http://www.unity3dstudent.com/2010/10/football-kick-power-bar/#
Answer by Statement · Mar 27, 2011 at 06:42 PM
It works perfectly on the first charge up but but won't reach its maximum charge on any attempts afterward.
It seems this could would never reset the charge, but I can't see any problems with it getting maximum charge. I am not a JS guru but I think you should call Shoot with StartCoroutine. I'll edit the code below (I reformatted the indentation and removed the comments, sorry about that) and maybe you can have a quick test with it?
private var thePower : float; private var increasing : boolean = false; private var shooting : boolean = false; var chargeSpeed : float = 1; var fullcharge : float = 10; var shotForce : float = 5; var target : Transform;
function Update () { if(!shooting && Input.GetButtonDown("Jump")) { increasing = true; }
if(!shooting && Input.GetButtonUp("Jump"))
{
increasing = false;
StartCoroutine(Shoot(thePower)); // Use StartCoroutine.
thePower = 0; // Reset the power after a shot.
}
if(increasing)
{
thePower += Time.deltaTime * chargeSpeed;
thePower = Mathf.Clamp(thePower, 0, fullcharge);
}
}
function Shoot(power : float) { shooting = true; target.rigidbody.AddForce(transform.forward power shotForce); yield; shooting = false; }
However, if you're willing to go with another approach we might reduce some code.
Code is tested and works on my machine.
var target : Rigidbody; // Target to shoot away. var timer : float = 1; // 10 means it takes 10 seconds for full force. var minForce : float = 10; // Minimum force applied on shot. var maxForce : float = 50; // Maximum force applied on shot. private var power : float = 0; // Power charge.
for (;;) yield Play(); // Enter a Coroutine loop instead of Update.
function Play() {
power = 0; while (!Input.GetButton("Jump")) { yield; } // Wait until press. while ( Input.GetButton("Jump")) { Charge(); yield; } // Charge until release. Shoot(); }
function Charge() { power = Mathf.Clamp01(power + (1 / timer) * Time.deltaTime); }
function Shoot() { var impulse = Mathf.Lerp(minForce, maxForce, power); target.AddForce(transform.forward * impulse, ForceMode.Impulse); }
thats is amazing thank you so much, and no worries about the reformatting it needed it lol i'm just wondering if it would possible to attach a glow or light of somekind that would increase in intensity as the charge goes up though,as it seems like some visual indication of the charging process would be helpfull
Sure, you could make an Update function that sets material color or something based on power. Power go from 0 to 1, which makes it ideal for interpolating values with Lerp ($$anonymous$$athf.Lerp, Vector3.Lerp, Color.Lerp etc)
function Update () { renderer.material.color = Color.Lerp(Color.white, Color.red, power); }
Or you could just set the color during Charge() so you only update it when you are charging. Also you'd want to reset it at the start of Play().