- Home /
Decreasing and increasing a SlowMotion powerup
I have created a very rudimentary slow motion ability in my game but I don't want it to be able to be used at all times.
The solution I have in my head the idea is to have a SlowMotionCredit bank which depletes with usage at a fast rate and then slowly replenishes itself.
The following code is attached to the 'player' but once I add the SlowMotionCredit related lines the script compiles on but the slowmotion then doesn't work.... Interesting to hear people's thoughts... cheers
var xspeed : float = 5;
var zspeed : float = 5;
var x : float;
var z : float;
public var SlowMotionCredit : float = 60; // SlowMotionCredit Initial starting figure
function Update () {
x = Input.GetAxis("Horizontal") * xspeed * Time.deltaTime;
z = Input.GetAxis("Vertical") * zspeed * Time.deltaTime;
transform.Translate(x, 0, z);
SlowmotionCreditUpdate() {
SlowMotionCredit +1; // replenish SlowMotionCredit at a rate of 1 per second
}
//SlowMotion Starts
if (Input.GetKeyDown ("space"))
Time.timeScale=0.15;
SlowMotionCredit - 12; // Deducting 12 Credits for each second of SlowMotion Time Spent
//Time resets
if (Input.GetKeyUp ("space"))
Time.timeScale=1;
}
I will have to add a if statement 22 to first check if the Slow$$anonymous$$otionCredit bank is greater than 0.
Answer by chris_taylor · Jul 24, 2013 at 01:30 AM
first off there is no {} on the
if (Input.GetKeyDown ("space"))
Time.timeScale=0.15;
SlowMotionCredit - 12; // D
So SlowMotionCredit -= 12 would get called every Update. What you need to do is change it to this
if (Input.GetKeyDown ("space") && SlowMotionCredit >= slowMoCost)// use a variable for cost that way its easy to edit
{
Time.timeScale=0.15;
SlowMotionCredit -= slowMoCost; // D
}
You should also add that to the Fixed update that way it lowers based off time and not how fast the FPS is
Chris - thanks for your help! It's getting closer!
Unfortunately it deducts the Slo$$anonymous$$oCost once irrespective of how long the player holds the space bar to stay in slo$$anonymous$$o mode.
if (Input.Get$$anonymous$$eyDown ("space") && Slow$$anonymous$$otionCredit > 0)// use a variable for cost that way its easy to edit
{
Time.timeScale=0.15;
Slow$$anonymous$$otionCredit -= slow$$anonymous$$oCost; // D
}
Is there anything you can suggest to make it deduct the cost for every second in slo$$anonymous$$o mode not the amount of times?
Cheers!
@IndieHood Your welcome, im here to help. change it to Input.Get$$anonymous$$ey ins$$anonymous$$d of Input.Get$$anonymous$$eyDown and that should help with that problem as Get$$anonymous$$eyDown only returns true when you first push the button down and Get$$anonymous$$ey returns true anytime the key is down
Your answer
Follow this Question
Related Questions
Calcule Player's Speed based on Time.timeScale 1 Answer
Time.timeScale = 0 did not prevent click 1 Answer
How would you find out if a rotation is between two values? 2 Answers
Help with my "speeding up time" script 1 Answer
Slow-Motion and Speed Boost - Jerky Movement Once Energy is Finished 1 Answer