- Home /
How can I change a float variable over time?
I have a simple script which changes a float variable in another script (via GetComponent) when an input is pressed. At the moment the variable changes instantly but I would like to be able to control the speed at which this float variable changes while the input is pressed. For eg. if I press the input for a short time the float starts to approach the value but only reaches it if the input is pressed for a long enough time. How can I do this? In the "if" statement if I change the "=" to a "-=" of "+=" it does change the float over time but I still can't control how fast it changes.
using UnityEngine;
using System.Collections;
public class HydraulicSuspension : MonoBehaviour {
public float lowRider = 0.1f;
public CarDynamics carDynamics;
//Use this before intialization
void Awake(){
carDynamics = GetComponent <CarDynamics>();
}
// Update is called once per frame
void FixedUpdate () {
//Check if Hydraulics is pressed
if(Input.GetAxisRaw("Hydraulics") == 1) {
carDynamics.suspensionTravelFront = lowRider;
carDynamics.SetCarParams();
}
}
}
Answer by Hamesh81 · Oct 22, 2012 at 09:04 AM
The first answer of this question solved everything for me. For anyone having trouble understanding how to set up a Mathf.lerp, I recommend you read this. It isn't as straight forward as one would think and it took me a while to get it. It is basically what whydoidoit suggested previously but it goes into a few simple examples as well. Hope that helps.
Answer by gribbly · Oct 13, 2012 at 06:15 AM
Take a look here:
http://docs.unity3d.com/Documentation/ScriptReference/Mathf.Lerp.html
You'll probably end up with something like:
function FixedUpdate () {
if(Input.GetAxisRaw("Hydraulics") == 1) {
carDynamics.suspensionTravelFront = Mathf.Lerp(min, max, Time.time);
carDynamics.SetCarParams();
}
}
...where min and max are float vars that you've set at the min and max range you want.
To control speed, you can do:
carDynamics.suspensionTravelFront = Mathf.Lerp(min, max, Time.time * speed);
...where speed is another float var.
Perfect! That's exactly what I was looking for. Thanks a lot :)
That should be Time.deltaTime added to a float initialised to 0 - not Time.time (which is the number of seconds elapsed since the game started playing and will rapidly be greater than 1 - meaning the Lerp has no effect).
Thanks a lot for your help $$anonymous$$ike. I'm finding it difficult to slow Time.deltatime when multiplying it by the speed variable. I've tried using a float value as small as 0.001 for the speed but it doesn't seem to be any slower that using a speed of 1.0. Any reasons for this?
Here it is:
using UnityEngine; using System.Collections;
public class HydraulicSuspension : $$anonymous$$onoBehaviour { public float offRoadRider = 0.3f; public float lowRider = 0.1f; public float speed = 0.001f; public bool reachedTop = false; public bool reachedBottom = false; public CarDynamics carDynamics; //Use this before intialization void Awake(){ carDynamics = GetComponent (); } // Update is called once per frame void FixedUpdate () { //Check if HydraulicsUp is pressed if(Input.GetAxisRaw("HydraulicsUp") == 1 && !reachedTop && !reachedBottom) { carDynamics.suspensionTravelFront = $$anonymous$$athf.Lerp(lowRider, offRoadRider, Time.deltaTime speed); carDynamics.suspensionTravelRear = $$anonymous$$athf.Lerp(lowRider, offRoadRider, Time.deltaTime speed); carDynamics.SetCarParams(); Debug.Log ("Up Pressed"); } }
Answer by MountDoomTeam · Oct 13, 2012 at 11:06 AM
This is a useful script for controlling fire rate:
http://docs.unity3d.com/Documentation/ScriptReference/Time-time.html
Your answer
Follow this Question
Related Questions
How Reset Variable After Loop 1 Answer
Start timer with trigger 1 Answer
Random time activate/deactivate object 1 Answer
Problem with time system and variables 1 Answer
OpenFeint float scores 2 Answers