math.Lerp not correct?
I have an AI Vehicle on a path and I want it to slow down from 40 to 15 smoothly using a box collider as a trigger. I tried doing some research and found that the math.lerp function can do what I need (I think). So I tried it but something isn't quite working. I have no errors in my script but the speed is still dropping from 40 to 15 instantly. Please help. Thank you.
My current script:
Answer by Firedan1176 · Jul 08, 2016 at 01:52 AM
The Time.deltaTime
field is the percentage of how far across the other 2 parameters you want it to be. Time.deltaTime is approximately 0.02, which means it is nearly 40 or 15, depending on how you are passing it. You will want to throw this into a Coroutine, because as of right now, you are only calling Mathf.Lerp
for 1 frame, and then stopping. You're going to want to make the Time.deltaTime
variable transition from 0 to 1 (1 = 100%). In your OnTriggerEnter, you can add a method call like this: StartCoroutine(SlowDown(0, 1));
. Then, you can create a coroutine like this:
IENumerator ChangeSpeed(float start, float end) {
float t = start;
while(t < end) {
phantomCar.GetComponent<MoveOnPath>().speed = Mathf.Lerp(start, end, t);
t += Time.deltaTime;
yield return null;
}
}
By doing this, you're evenly transitioning from start
to end
based on t
, which is a percent. You can pass in different numbers as start
and end
to get different results (speed up, slow down).
Thank you for your quick response! I forgot that I was only calling one frame with the on trigger enter. I will experiment with this and see how it goes. Thanks again!
This is what I got when I added the code you suggested. I cant figure out why there is an error after the IEnumerator.
An IENumerator is different from a void method, because it needs to return something. So in your IENumerator, you need to add this at the end of your while loop:
yield return null;
You can also yield for a certain number of frames or seconds:
yield return new WaitForSeconds(1f); //Waits for 1 second before executing code below this line
yield return new WaitForEndOfFrame();
yield return new WaitForFixedUpdate();
I apologize, because I didn't include this in my answer. I will do that now.
Awesome, that did it. I'm super new to understanding C# I will mess around with this so I can make the car move from 40 to 15 then back to 40. Thank you!
So I got the car to slow down by saying StartCoroutine(SlowDown(0,40)); that was the only way to gradually slow the vehicle down from 40, changing numbers in the public floats didnt do anything, so now it stops at 0 and that's it. I tried StopCoroutine but that didnt do anything. I also tried starting my OnTriggerExit function but that didnt do anything either. It just sits there. Thanks for your help and patience.
Think of StartCoroutine
as starting a stopwatch. Stopping the stopwatch will halt it from doing anything. Therefore StopCoroutine is only necessary if you're doing something, like a while loop in the coroutine, and you want to stop it outside of the coroutine (such as Update). When you start a Coroutine, you're executing code in a different thread. Therefore, we can stop the thread.
In your case, if you want the car to speed up, you'd want to do the exact thing with another coroutine, such as SpeedUp()
. $$anonymous$$ost of the code I put is just pseudocode, and it's not tested. So it's hit-and-miss, but you should get the idea. If I answered your main question, can you accept it as an answer? You can always ask another question pertaining to these other problems,
Your answer
Follow this Question
Related Questions
OnTriggerExit2D doesn't work when one of gameObject setActive false. 0 Answers
C# OnTriggerEnter Issue 3 Answers
OnTriggerEnter For Parking 1 Answer
Crane Pick up script - setting new parent 0 Answers
Access variable from different class 1 Answer