- Home /
How do I get a value to increment smoothly?
How do I get a variable that’s set to a value to increment smoothly (or smoothly ease/ fade) to a target value? Such as speeding or slowing a plane during flight. Here’s my code below:
#pragma strict
var speed : int;
var maxSpeed : int = 100;
var minSpeed : int = 0;
var startSpeed : int = 50;
var camera : GameObject;
var zoomCamera : GameObject;
var enemyKillRatio : int = 0;
var health : int = 100;
var reset : boolean = false;
function Update () {
//start speed
speed = startSpeed;
//constant forward
transform.Translate(0, 0, speed * -1 * Time.deltaTime); //speed set at 50 here.
//movments; speedup
if (Input.GetKey("c")) {
//smoothly incrimint speed up by 1.
speed = ++ speed;//needs incrment speed.
transform.Translate(0, 0, speed * -1 * Time.deltaTime);
}
//movements; slowdown
if (Input.GetKey("v")) {
//smoothly deincrament speed by 1.
speed = -- speed;//need increment speed.
transform.Translate(0, 0, speed * Time.deltaTime);
}
//limit speed
if(speed >= maxSpeed){
speed = maxSpeed;
}
if(speed >= minSpeed){
speed = minSpeed;
}
}
How do I get my movement speed up and movement slow down to smoothly ease to a max or a minimum value? The output of the code so far allows the player fly forward. When c key is pushed, the player goes faster (until maxSpeed = 100). When v key is pushed the player stops instantly (when minSpeed = 0).
Answer by Rambit · Jul 26, 2013 at 11:23 PM
Maybe you can try to use Mathf.Lerp to smooth out?
Something like this: (untested)
speed = Mathf.Lerp (speed, minSpeed, smoothTime);
when you press the C key. And then this to smooth to max speed:
speed = Mathf.Lerp (speed, maxSpeed, smoothTime);
I've tryed everything. Nothing seems to work.
#pragma strict
var speed : int;
var maxSpeed : int = 100;
var $$anonymous$$Speed : int = 0;
var startSpeed : int = 50;
var camera : GameObject;
var zoomCamera : GameObject;
var enemy$$anonymous$$illRatio : int = 0;
var health : int = 100;
var reset : boolean = false;
//var smoothTime : float = 1.0;
function Update () {
//start speed
speed = startSpeed;
//constant forward
transform.Translate(0, 0, speed * -1 * Time.deltaTime); //speed set at 50 here.
//movments; speedup
if (Input.Get$$anonymous$$ey("c")) {
//smoothly incri$$anonymous$$t speed up by 1.
//speed = ++ speed;
//speed = $$anonymous$$athf.Lerp (speed, maxSpeed, smoothTime);
//transform.Translate(0, 0, speed * -1 * Time.deltaTime);
//transform.Translate(0, 0, $$anonymous$$athf.Lerp(speed, maxSpeed, smoothTime) * -1* Time.deltaTime);
}
//movements; slowdown
if (Input.Get$$anonymous$$ey("v")) {
//smoothly deincrament speed by 1.
//speed = -- speed;
//speed = $$anonymous$$athf.Lerp (speed, $$anonymous$$Speed, smoothTime);
//speed = $$anonymous$$athf.Lerp($$anonymous$$Speed, maxSpeed, timePassed/accelerateTime);
//speed = $$anonymous$$athf.Lerp (speed, $$anonymous$$Speed, smoothTime);
//transform.Translate(0, 0, $$anonymous$$athf.Lerp(speed, $$anonymous$$Speed, smoothTime)* Time.deltaTime);
//transform.Translate(0, 0, speed * Time.deltaTime);
}
//limit speed
if(speed >= maxSpeed){
speed = maxSpeed;
}
if(speed <= $$anonymous$$Speed){
speed = $$anonymous$$Speed;
}
}
I tried it and it worked for me by doing:
transform.Translate(0, 0, speed * -1 * Time.deltaTime);
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.J))
{
speed = $$anonymous$$athf.Lerp (speed, maxSpeed, smoothTime * Time.deltaTime);
}
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.$$anonymous$$))
{
speed = $$anonymous$$athf.Lerp (speed, $$anonymous$$Speed, smoothTime * Time.deltaTime);
}
In the Update() function. Also make sure to make the speed variables floats, because $$anonymous$$athf.Lerp doesn't take ints. $$anonymous$$ake sure to add * Time.deltaTime after smoothTime so it will be frame rate independant.
Thank you for the reply. I did exactly as you did and found the results the same. Nothing seems to be working. Could it be something in the Inspector or Input? Here’s my code:
#pragma strict
var speed : float;
var maxSpeed : float = 100;
var $$anonymous$$Speed : float = 0;
var startSpeed : float = 50;
var camera : GameObject;
var zoomCamera : GameObject;
var enemy$$anonymous$$illRatio : int = 0;
var health : int = 100;
var reset : boolean = false;
//-----------------------------
var smoothTime : float = 0.5;
//-----------------------------
function Update () {
//start speed
speed = startSpeed;
//constant forward
transform.Translate(0, 0, speed * -1 * Time.deltaTime); //speed set at 50 here.
//-----------------------------------------------------------------------------------------------
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.J))
{
speed = $$anonymous$$athf.Lerp (speed, maxSpeed, smoothTime * Time.deltaTime);
}
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.$$anonymous$$))
{
speed = $$anonymous$$athf.Lerp (speed, $$anonymous$$Speed, smoothTime * Time.deltaTime);
}
//------------------------------------------------------------------------------------------------
//limit speed
if(speed >= maxSpeed){
speed = maxSpeed;
}
if(speed <= $$anonymous$$Speed){
speed = $$anonymous$$Speed;
}
}
Answer by tw1st3d · Jul 27, 2013 at 04:21 PM
var lastInc = 0.0;
var toInc = 0;
while(Input.GetKeyDown(KeyCode.C))
{
if(( Time.time > lastInc + 0.05))
{
toInc++;
lastInc = Time.time;
}
}
Set it up to wait .05 seconds in between incrementation.
I want to thank you for the reply; nevertheless, your code makes my browser crash.
Try increasing the time gap required to perhaps 0.1 seconds
Also, this is untested, but you could try
lastInc = lastInc + (1 * Time.deltaTime);