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 /
avatar image
0
Question by Higgenbothom · Oct 12, 2013 at 08:30 PM · rotationbuglerp

Annoying smooth rotation jitter bug

Hi all,

I'm working on a script that does semi-random movement of animals like seagulls, whales and so on. I made it so, that the entity rotates smoothly to it's next target (with Vector.Lerp). For some reason, it sometimes starts jittering around. I assume it's the Vector3.lerp that does this, for some reason. When it does start to spin, it's almost always straight up or straight down.

I tried to solve it by changing some stuff allready, so take a look at the list below before posting.

  1. Do the Rotation in FixedUpdate instead of Update.

  2. Take out the Time.deltaTime component in the Vector3.Lerp function.

  3. Rotate the entity a bit after generating a new Destination.

  4. Null Rotation and SmoothRotation after generating a new Destination.

  5. After using LookAt and saving the rotation, set the rotation back to what it was before.

  6. Muck about with the third component of the Lerp function.

  7. While doing the above, keep switching between Lerp and Slerp.

None of the above worked. Using Lerp instead of Slerp does help.

You can see the script in action here.

The script:

 using UnityEngine;
 using System.Collections;
 
 public class Static_RandomMovement : MonoBehaviour {
     
     //////////////////////////////////////////////
     //    Script:        Static_RandomMovement.cs    //
     //    Project:    Pirate Project                //
     //    Scripter:    De Yuang Ann Wijning        //
     //                                            //
     //    Description:                            //
     //    Handles semi-random movement of animals    //
     //    such as whales and seagulls.            //
     //    Sometimes fucks up and jitters around.    //
     //////////////////////////////////////////////
     
     public bool ElectNewPos         = true;            // Request new Destination
     private Vector3 Destination     = Vector3.zero;    // Point the entity moves towards
     private Vector3 SmoothRotation     = Vector3.zero;    // Smoothed out Rotation
     private Vector3 Rotation         = Vector3.zero;    // Raw Rotation
     public Vector3 Offset             = Vector3.zero; // Entity's Offset
     public bool CustomOffset         = false;        // Does the Entity have a costum offset?
     public bool FlatPlane             = false;        // If you turn this on, the entity doesn't change height.
     public float MinimumDistance     = 2F;            // Minimum distance the new Destination has to be from the current position
     public float xFarLimit             =-10F;            // Smallest X
     public float xNearLimit         = 10F;            // Biggest X
     public float yFarLimit            =-10F;            // Smallest Y
     public float yNearLimit         = 5F;            // Biggest Y. If the Entity is a sea creature (such as a whale), make sure this is lower than the sea itself.
     public float zFarLimit             =-10F;            // Smallest Z
     public float zNearLimit         = 10F;            // Biggest Z
     public float Speed                 = 10F;            // Entity's movement speed
     public GameObject TestObject     = null;            // Cube Target
     
     // Take this out once jittering has stopped
     public int SuccesCount            = 0;            // How many times has the seagull succesfully reached the target?
     public int FailCount            = 0;            // How many time did it fuck up above task?
     
     void Start(){
         
         Rotation = transform.eulerAngles;
         if(xFarLimit>xNearLimit){
             xFarLimit = xNearLimit;    
         }
         if(yFarLimit>yNearLimit){
             yFarLimit = yNearLimit;    
         }
         if(zFarLimit>zNearLimit){
             zFarLimit = zNearLimit;    
         }
         if(CustomOffset == false){
             Offset = transform.position;
         }
         GenerateNewPos ();
     }
     
     void GenerateNewPos(){
         
         ElectNewPos = true;
         Destination = Vector3.zero;
         SmoothRotation = Vector3.zero;
         Rotation = Vector3.zero;
         
         if(FlatPlane == true)
             Destination = new Vector3(Random.Range (xFarLimit, xNearLimit), transform.position.y, Random.Range (zFarLimit, zNearLimit));
         else
             Destination = new Vector3(Random.Range (xFarLimit, xNearLimit), Random.Range (yFarLimit, yNearLimit), Random.Range (zFarLimit, zNearLimit));
         
         //Failsave
         if(Vector3.Distance (transform.position, Destination) < MinimumDistance){
             print ("Static_RandomMovement.cs notice: Distance is too close, generating a new destination point.");
             GenerateNewPos ();
         }
         else{
             ElectNewPos = false;
             TestObject.transform.position = Destination;
             Move ();
         }
     }
     
     void FixedUpdate () {
     
         if(ElectNewPos == true || Vector3.Distance(transform.position, Destination) < 1F){
             SuccesCount ++;
             GenerateNewPos ();
             print ("Static_RandomMovement.cs notice: Destination reached. Generating new roam target.");
         }
         else if(transform.position.y < yFarLimit - 2F || transform.position.y > yNearLimit + 2F){
             FailCount ++;
             print ("Static_RandomMovement.cs Alert: Torpedo Alert!");
             transform.LookAt(Destination);
             transform.Translate (Vector3.forward * Speed * Time.deltaTime);
         }
         else{
             Move();
         }
     }
     
     void Move(){
         
         // Calculate Rotation
         Rotation = transform.eulerAngles;
         transform.LookAt(Destination);
         SmoothRotation = Vector3.Lerp(Rotation, transform.eulerAngles, Speed * 0.01F);
         
         // Rotate
         transform.eulerAngles = SmoothRotation;
         
         // Move
         transform.Translate (Vector3.forward * Speed * Time.deltaTime);
     }
     
     void OnGUI(){
         
         // Take this out once jittering has stopped
         GUI.Label(new Rect(Screen.width * 0.88F, Screen.height * 0.88F, Screen.width * 0.11F, Screen.height * 0.11F), "Succes Count: " + SuccesCount);
         GUI.Label(new Rect(Screen.width * 0.88F, Screen.height * 0.77F, Screen.width * 0.11F, Screen.height * 0.11F), "Fail Count: " + FailCount);
         
     }
 }

Thank you for reading.

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

Answer by DarkPixel · Oct 12, 2013 at 11:40 PM

Probably related to Gimbal Lock. You should'nt use euler angles rotation interpolation because of that. If you switch to Quaterion.Lerp for your rotation, I think you will solve your problem

Comment
Add comment · Show 1 · 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 Higgenbothom · Oct 13, 2013 at 10:45 AM 0
Share

Switching from eulerAngles to Quaternions fixed it for me. Thanks!

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

16 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

Related Questions

Unity Quaternion Problem (Should be A Bug?) 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Simple lerp roation problem 1 Answer

Mathf.Lerp working in one direction but not the other 1 Answer

Camera to follow the player in the form of radius 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