- Home /
Mathf repeat at the end? help!
Hello! I need to make it when the Mathf.lerp function reaches the set value, that is, at the end, it repeats
How could I do this?
Thank you!
ClutchTorque = Mathf.lerp (MaxClutchTorque2, MinClutchTorque, ClutchSpeed);
What are you trying to achieve? I mean repeating a linear interpolation isn't very useful. You approach the value until you reach it. Repeating the lerp isnt going to do anything because you are already at the value...Could you explain your idea a little further ?
I have a video for reference, look when the car makes gear changes (second 0:11 of the video)
I am trying to imitate that with $$anonymous$$atf.lerp
that is, that it reaches the target value and goes down even more in a fast way and short
Answer by metalted · Mar 19, 2020 at 09:25 PM
So after your explanation about the gears rpm, it became much clearer what your objective was. I just created this bit of code on the fly, and i cant test it at the moment. But this would be how i would start tackling the problem:
public struct Gear{
float minRPM;
float maxRPM;
public Gear(float _minRPM, float _maxRPM){
minRPM = _minRPM;
maxRPM = _maxRPM;
}
}
public class GearShift : MonoBehaviour {
private Gear[] gears = new Gear[6];
private float currentRPM = 0;
private int currentGear = 0;
private float revUpSmoothTime = 0.3f;
private float gearChangeSmoothTime = 0.1f;
private bool rpmUP = true;
public void Start(){
gears[0] = new Gear(0f, 3000f);
gears[1] = new Gear(1200f, 3200f);
gears[2] = new Gear(1400f, 3400f);
gears[3] = new Gear(1600f, 3600f);
gears[4] = new Gear(1800f, 3800f);
gears[5] = new Gear(2000f, 4000f);
}
public void Update(){
if(rpmUP){
//Move the rpm to the max rpm of the current gear.
currentRPM = Mathf.SmoothDamp(currentRPM, gears[currentGear].maxRPM, ref rpmVelocity, revUpSmoothTime);
//If we reach the max rpm of this gear:
if(gears[currentGear].maxRPM - currentRPM < 0.01f)
{
currentGear++;
rpmUP = false;
}
}
else
{
//Move the rpm to the min rpm of the current gear.
currentRPM = Mathf.SmoothDamp(currentRPM, gears[currentGear].minRPM, ref rpmVelocity, gearChangeSmoothTime);
//If we reach the min rpm of this gear:
if(currentRPM - gears[currentGear].minRPM < 0.01f)
{
//Start rev up again:
rpmUP = true;
}
}
}
}
We just define the different gears and their rpms. Move up to the max rpm, change gear, move down to the min rpm when changing, go up to max rpm, change gear, repeat... The change of the rpms is a bit more complicated than this because the closer you get to the max rpm of that gear, the slower the rpms will go up so you might want to factor that in. If you just want to keep it simple for now, like this script, you definitely need 2 smoothing/lerp functions: one for rpm up and one for rpm down.
Your answer
![](https://koobas.hobune.stream/wayback/20220612225312im_/https://answers.unity.com/themes/thub/images/avi.jpg)