- Home /
How to smootlhy change values in amout of time?
I know it can be kinda easy, but...
I'm trying to make my character run if I'll press "Fire1". So the speed of my character should smoothly change into higher value, from 6 to 15.
I tried to do it by Animation Curves, but it seems like i can't apply 2 other animations to 1 object at the same time (like for example run and crouch).
So... is it any other good way, that allowys me to take control above evertything (time, when value changes)? Och, and please comment everything, cause I'm new in programming.
Thanks!
Answer by dhendrix · Apr 02, 2010 at 02:46 PM
Do you know about Time.deltaTime? (the time that has elapsed since last update)
var speedMin = 6.0; var speedMax = 15.0; var accelerateTime = 3.0; private var currentSpeed = 0.0; private var timePassed = 0.0;
function FixedUpdate() { if (Input.GetAxis("Fire1")) { timePassed += Time.deltaTime; currentSpeed = Mathf.Lerp(speedMin, speedMax, timePassed/accelerateTime);
} }
The public vars can be adjusted in the editor, you can adjust "accelerateTime", and the character should speed up faster.
How you apply this "currentSpeed" value depends on how you are moving your character. If using a characterController, this link may help http://unity3d.com/support/documentation/ScriptReference/CharacterController.Move.html
Its nice, but it's not that detailed, as i want to. It increase speed. Okay. But I want something, like I choose the time, when the variable change into max, not like I choose how much it increases. In my GDD i have "speed up from 6 to 15 in 3 seconds" and I cannot make it by your way.
I've edited my answer to reflect what you seem to want. You may want to also use Input.GetButtonUp("Fire1") to reset "timePassed" to 0, when the player stops running.
Answer by Rafal Sankowski · Apr 02, 2010 at 09:04 PM
That does not work. I have code like that:
if (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0){ timePassedSpeed += Time.deltaTime; currentSpeed = Mathf.Lerp(speedMin, speedMax, timePassedSpeed/accelerateTime); }
else{ timePassedSpeed += Time.deltaTime; currentSpeed = Mathf.Lerp(speedMax, speedMin, timePassedSpeed/accelerateTime); }
So i simply want to accelerate, when any arrow key is down, and decrease speed, when it's not. Its smooth, when variable "currentSpeed" do not equals speedMin or speedMax. When it equals that and I try to accelearate or decrease speed, variable changes immadietly from 6 to 15, or from 15 to 6.
Answer by thieum · Sep 08, 2011 at 11:39 AM
Hi, what you need is Mathf.SmoothDamp function. Then it shifts from var A to var B through time. there is a tricky thing with this function using ref yVelocity to (eventually) smooth the curve between A and B...
so first declare : private float (or var) y Velocity = 0; public float transTime(or whatever) = 1f; (fixed time to go from a to b, I guess we can use a function too, but should be sufficient)
then in your Update: if (Input.GetKey (KeyCode.UpArrow)) { currentSpeed = Mathf.SmoothDamp (speedMin, speedMax, ref yVelocity, transTime);
I hope it helps...