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 /
avatar image
0
Question by Kristov · Jun 20, 2011 at 04:19 AM · lerp

Mathf.LerpAngle working once, but not again

Hi all,

I have a problem where the function on a script calling Mathf.LerpAngle works once, but not again after that. When I instantiate a particular enemy, I want it to, at random points, turn between it's current eulerAngles.y and a random one between 0 and 360. This works the first time, when I spawn the enemy, but after that it just instantly hits the random angle I picked. Anyway, here's excerpts the script:

 private var turning : boolean = true;
 private var minAngle : float;
 private var maxAngle : float;
 private var turnTimer : float;
 
 function Update()
 {
     if(turning == true)
     {
         var currentAngle : float = Mathf.LerpAngle(minAngle, maxAngle, Time.time);
         transform.eulerAngles.y = currentAngle;
         if((maxAngle - transform.eulerAngles.y) < 1 && (maxAngle - transform.eulerAngles.y) > -1)
         {
             turning = false;
             turnTimer = Time.time + Random.Range(3.0, 8);
         } 
     }
     else if(turning == false && turnTimer < Time.time)
     {
         minAngle = transform.eulerAngles.y;
         maxAngle = Random.Range(0.0, 360);        
         turning = true;
     }
 }

Any help would be greatly appreciated!

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
2
Best Answer

Answer by Bunny83 · Jun 20, 2011 at 04:37 AM

What do you expect Time.time to return? It returns the seconds since you've started your game. Like every LERP (linear interpolation) the "t" value have to be between 0.0 and 1.0

That's why Time.time will work... but only during the first second. After that the value will be clamped to 1.0

I guess you want Mathf.MoveTowardsAngle.

Comment
Add comment · Show 5 · 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 Kristov · Jun 20, 2011 at 05:21 AM 0
Share

Thanks, that worked perfectly. I thought the Time.time thing was strange too, but as it was in the API here http://unity3d.com/support/documentation/ScriptReference/$$anonymous$$athf.LerpAngle.html I thought I just didn't know what it was supposed to do. Also, thanks for explaining what lerp actually stood for, cause that was annoying me a bit :D Thanks again!

avatar image Bunny83 · Jun 20, 2011 at 05:37 AM 0
Share

:D yeah, some of the examples in the docs don't make much sense...

If you want to know a bit more about lerp look here:
http://en.wikipedia.org/wiki/Lerp_%28computing%29

avatar image roamcel · Dec 02, 2011 at 09:14 AM 0
Share

@Bunny83 thanks for the references. I am curious about something though: anybody happens to know the differences between '$$anonymous$$athf.LerpAngle' and '$$anonymous$$athf.$$anonymous$$oveTowardsAngle', at this point? Is it just that the latter accept degrees per second rather than time?

avatar image Bunny83 · Dec 02, 2011 at 12:37 PM 2
Share

@roamcel: The difference is that lerp is always based on a t-value (not time) this value deter$$anonymous$$es the position between start(t==0) and end(t==1).

Usually the start and end values doesn't change during lerp. Imagine two points in space and connect them with a straight line.

However some people use lerp for a decelerated movement by using the current position as start (so it's different each time you call lerp) and a small fix t-value. If you use for example 0.2 for t that would mean each frame you move 20% of the remaining distance towards the end. Since the distance gets smaller over time the 20% movement also gets smaller. That's why it's decelerated. Actually you will never reach the target that way, because you always move just 20% of the remaining distance. It can get very very small <0.00001 so visually there's no change.

$$anonymous$$oveTowardsAngle does something similar like the normal lerp but it uses a constant speed (that's the max delta value) which tells the function how much it's allowed to move each frame. It's almost the same as moving it "manually" each frame maxDelta-amount towards the target. The only difference is that is ensures to stop at the target ;) Only in the last step it will reduce the amount of movement to stop at the target.

The OP wanted a true linear movement. If you use lerp you have to calculate the t-value accordingly and move it from 0 to 1 over time.

If you want to move a value from start to end in a specific timespan, use lerp. If you want to move a value from start to end at a specific speed use $$anonymous$$athf.$$anonymous$$oveTowards or $$anonymous$$athf.$$anonymous$$oveTowardsAngle.

avatar image roamcel · Dec 02, 2011 at 02:06 PM 0
Share

Precious explanation. Thanks a lot.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Mathf.Lerp won't stop snapping 2 Answers

Smooth Camera Angle Scroll 1 Answer

Lerp isn't completing itself 3 Answers

Lerp doesn't work after using a lerp? 1 Answer

How do I add multiple points using Lerp? 3 Answers


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