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 DayyanSisson · Aug 04, 2012 at 05:01 AM · aiquaternionrotatesmoothslerp

Doesn't Add Rotation Over 180

I'm using this script to make an AI walk around smoothly instead of making jerky turns. Here's ta script I whipped up in a couple minutes:

  public float speed;
  public float rotateSpeed;
 
  public float minWalkTime;
  public float maxWalkTime;
  public float minRotateAmount;
  public float maxRotateAmount;
 
  public bool rotate;
 
  public float timeToNewDir;
 
  private Transform student;
  private CharacterController controller;
 
  void Awake () {
 
        student = transform;
        controller = student.GetComponent<CharacterController>();
  }
 
  void Update () {
  
         Wander();
         if(rotate){
                Rotate(minRotateAmount, maxRotateAmount);
         }
  }
 
  void Wander () {
 
         float idleTime = Random.Range(minWalkTime, maxWalkTime);
         if(Time.time > timeToNewDir){
                timeToNewDir = Time.time + idleTime;
               rotate = true;
         }
         var fwd = student.TransformDirection(Vector3.forward);
         controller.SimpleMove(fwd * speed);
  }
 
  void Rotate (float minAmount, float maxAmount) {
 
         float rotateAmount = Random.Range(minAmount, maxAmount);
         Quaternion newRot = student.rotation;
         newRot.y += rotateAmount;
         student.rotation = Quaternion.Slerp(student.rotation, newRot, rotateSpeed * Time.deltaTime);
 
         if(student.rotation == newRot){
                rotate = false;
         }
  }

The problem is, once "y" has become 180, the AI no longer turns, even though he should. The script doesn't stop executing, the rotation just isn't going though. Does it have to do with it being a Quaternion?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by aldonaletto · Aug 04, 2012 at 11:42 AM

@Akill is right: the quaternion components XYZ have nothing to do with those nice angles you see in the Inspector (they are the localEulerAngles).
But your code had other problems too: Rotate will be called every frame, defining new orientations each time, and you probably would never get an equality to set rotate to false due to the limited floating point precision.
A better approach is to define the desired angle (yAngle) in Rotate and make the character always follow this angle smoothly at Update:

... private CharacterController controller; // declare these new variables: private Vector3 curEuler; // holds current rotation angles private float yAngle; // holds desired angle around Y

void Awake () {

    student = transform;
    controller = student.GetComponent<CharacterController>();
    curEuler = student.eulerAngles; // initialize the new variables
    yAngle = curEuler.y;

}

void Update () {

     Wander();
     // smoothly follows the desired yAngle:
     curEuler.y = Mathf.Lerp(curEuler.y, yAngle, rotateSpeed * Time.deltaTime);
     student.eulerAngles = curEuler;

}

void Wander () {

     float idleTime = Random.Range(minWalkTime, maxWalkTime);
     if(Time.time > timeToNewDir){
           timeToNewDir = Time.time + idleTime;
           Rotate(); // just set the new direction
     }
     var fwd = student.TransformDirection(Vector3.forward);
     controller.SimpleMove(fwd * speed);

}

void Rotate (float minAmount, float maxAmount) {

     float rotateAmount = Random.Range(minAmount, maxAmount);
     // just add rotateAmount to yAngle modulo 360:
     yAngle = (yAngle + rotateAmount) % 360;

}

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 DayyanSisson · Aug 04, 2012 at 07:42 PM 0
Share

That worked better than I thought it would! Thanks, that makes sense.

avatar image
2

Answer by Akill · Aug 04, 2012 at 11:14 AM

Yes, Quaternions can not be used like this. You will need to create a new Quaternion which will hold the new rotation you want. To apply this rotation to the previous rotation, you multiply the 2 quaternions together.

 void Rotate (float minAmount, float maxAmount) {

     float rotateAmount = Random.Range(minAmount, maxAmount);
     Quaternion newRot = student.rotation;
 
     newRot*= Quaternion.Euler(0,rotateAmount,0);
     student.rotation = Quaternion.Slerp(student.rotation, newRot, rotateSpeed * Time.deltaTime);

     if(student.rotation == newRot){
            rotate = false;
     }
 }

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

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

9 People are following this question.

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

Related Questions

Quaternion.Slerp question, rotating back to original 1 Answer

Quaternion.Slerp problem... 1 Answer

Quaternion.slerp rotates instantly. 2 Answers

Choppy rotation of character around y-axis 1 Answer

Quaternion based slerp to make homing projectile doesn't turn 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