Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by nsane808 · Jul 08, 2016 at 01:43 AM · c#colliderontriggerenterontriggerexitmathf.lerp

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:

alt text

capture.png (17.8 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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).

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nsane808 · Jul 08, 2016 at 04:08 AM 0
Share

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!

avatar image nsane808 · Jul 08, 2016 at 03:45 PM 0
Share

This is what I got when I added the code you suggested. I cant figure out why there is an error after the IEnumerator.

alt text

capture.png (19.1 kB)
avatar image Firedan1176 nsane808 · Jul 08, 2016 at 04:13 PM 0
Share

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.

avatar image nsane808 · Jul 08, 2016 at 04:40 PM 0
Share

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!

avatar image nsane808 · Jul 08, 2016 at 06:33 PM 0
Share

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.

avatar image Firedan1176 nsane808 · Jul 30, 2016 at 07:38 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

184 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges