Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed May 10, 2013 at 07:44 AM by Fattie for the following reason:

Duplicate Question

avatar image
0
Question by Vox Nephila · May 10, 2013 at 03:48 AM · c#lerpeuleranglesmathf

Mathf.Lerp won't stop snapping

As is, this code will cause it to snap to the angle instead of lerp towards it. Smooth is set to 5 currently in the demo and it seems that the higher I put that number, the more angle I will get. Please play the demo to fully see the issue. Demo

I have tried Mathf.Lerp. Also, without the smooth value it will only ever so slightly budge. As if it's only doing it on one frame but I have checked and it's constantly being accessed.

         if (ship.position.x > 5) {
             angle = Mathf.LerpAngle(camera.rotation.y, 40f, Time.deltaTime * smooth);
             camera.eulerAngles = new Vector3(10, angle, 0);
         }
         else if (ship.position.x < 2) {
             angle = Mathf.LerpAngle(camera.rotation.y, -40f, Time.deltaTime * smooth);
             camera.eulerAngles = new Vector3(10, angle, 0);
         }
         else {
             angle = Mathf.LerpAngle(camera.rotation.y, 0f, Time.deltaTime * smooth);
             camera.eulerAngles = new Vector3(10, angle, 0);
         }


Comment
Add comment · Show 2
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 Vox Nephila · May 10, 2013 at 04:36 AM 0
Share

Figured it out.. just needed to change camera.rotation.y to camera.eulerAngle.y. Was just something I overlooked.

avatar image robertbu · May 10, 2013 at 04:38 AM 0
Share

I converted your 'Answer' into a comment. Answers are reserved to attempts to answer a question. As for using eulerAngles, you may have issues because the representation is not fixed.

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by robertbu · May 10, 2013 at 04:33 AM

'rotation' is not what you thing it is. Please read my comments and suggestions about Quaternions and eulerAngles in my answer to this question:

http://answers.unity3d.com/questions/415717/rotate-an-element-with-exactly-value.html

Even if you change the code to use eulerAngles instead of rotation, you cannot depend on any specific eulerAngle representation. You could assign (180,0,0) to eulerAngles and immediately read it back and get (0,180,180) (the same physical rotation).

Comment
Add comment · 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
0

Answer by gharbill · May 10, 2013 at 05:01 AM

This is a common mistake in using Lerp. Lerp functions actually do not smooth the value towards the target automatically. they work like a slider, you have to manually change the slider value yourself and you can do it in your code. the slider is the t value which is a float and should be between 0 and 1. here is how Lerp functions:

LerpAngle (a : float, b : float, t : float)

it looks at t vlaue and gives you a number between "a" and "b". if "t" is 0 or less it returns "a", and if "t" is 1 or more, it returns "b" and if "t" is between 0 and 1 it gives you a number between "a" and "b" and uses "t" as the ratio.

what you did in your code is using Time.deltaTime * smooth as t value which if used in update function, it is pretty much a constant. so Lerp will always return same number.

if you want your angle to change you have to give "t" parameter a value which increases/decreases every frame, so you have to use Time.time instead and have a temporary variable as "now". here is a simple implementation:

 private var now: float;
 private var letMove: boolean = flase;
 
 function myMove(){ // call myMove on this gameObject
                    // and it will start movement to x=10
 
    now = Time.time;
    letMove = true;
 
 } 
 
 function Update(){
 
    if (letMove){
 
       transform.position.x = Mathf.Lerp(0,10,(Time.Time - now));
 
    } 
 
 
 }
Comment
Add comment · Show 7 · 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 robertbu · May 10, 2013 at 05:11 AM 1
Share

His use of Lerp() is found in many UA answers. It's kinda a "misuse" of the Lerp() in that it does not (as you point out) increment 't' from 0 to 1, and it produces an eased motion rather than a linear one. Typically you see in a line like:

 transform.position = Vector3.Lerp(transform.position, target.position, speed * Time.deltaTime);
avatar image Vox Nephila · May 10, 2013 at 05:51 AM 0
Share

I was actually going for an eased look, and I achieved that so I will definitely read over these answers and try to understand what you're talking about, but I have gotten the functionality I am looking for now.

avatar image gharbill · May 10, 2013 at 06:04 AM 0
Share

Got it. yes you are right. So which one do you recommend?

avatar image robertbu · May 10, 2013 at 06:16 AM 1
Share

@gharbill - It depends on what you need. The big drawback of this "misuse" is that it does not cover the distance in any specified time nor at any specified speed. So you are stuck eyeballing the behavior to what feels right. Using a timer allows the Lerp() to cover the distance in a specified amount of time. And you can still ease the motion (and arrive on time) if you put the timer through an easing function. And then there is the lesser known cousin $$anonymous$$oveTowards() which allows the object to move at a specified speed.

I think the "misuse" gets used so much on UA because a) it is easy to write, and 2) it often feels better...more natural.

avatar image gharbill · May 10, 2013 at 06:24 AM 0
Share

Thanks, so i stick with the official Lerp()instruction.

Show more comments

Follow this Question

Answers Answers and Comments

15 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

Related Questions

Lerping & OnCollisionEnter - how do i lerp without update?? 1 Answer

Rotation jumping to a different rotation after rotating a certain amount 0 Answers

how to use time on lerp 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 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