• 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
Question by valiorin · Feb 02, 2014 at 12:07 AM · rotationvector3aiquaternion

getting AI to look Left, then Right, then resume path

Ok. So this is my second problem today.. You all were great help earlier so here goes. I have AI pathfinding, and I want the AI to stop when the reach a waypoint, look left and right, then resume their pathfinding. Of course I want this to be a smooth interpolation left and then right and then back to forward. I've been looking at all sorts of documentation and other questions about turning but none seem to quite work with my problem, but maybe I'm just missing something.
So here is my completely broken code, just to see where I'm coming from..

//look left

newRotation = Quaternion.LookRotation(targetLeft - transform.position);

     while(transform.rotation != newRotation)
         {
         transform.rotation = Quaternion.Lerp(transform.rotation, newRotation,Time.deltaTime);

         Debug.Log("Looking Left");
         yield return null;
         }
 //look right
     newRotation = Quaternion.LookRotation(targetRight - transform.position);
     while(transform.rotation != newRotation)
         {
             transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, Time.deltaTime);
             Debug.Log("Looking Right");
             yield return null;
         }
Comment

People who like this

0 Show 1
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 valiorin · Feb 02, 2014 at 10:11 PM 0
Share

So no help so far... It isn't very urgent, as it's more of a polish thing at this point, but any and all help is appreciated, and I seem to be stuck...

1 Reply

  • Sort: 
avatar image

Answer by Dead Lincs · Feb 03, 2014 at 12:13 AM

If it is in C# try something a little like this:

 void Start(){
         StartCoroutine (LookSequence); //Move this to a point in your code when the event needs to be triggered
     }
 
     public IEnumerator LookSequence(){
         //Stop the Ai from moving
         yield return new WaitForSeconds (1); //This code makes the script stop for 1 second. You can use float values here (for example 0.5F)
         LookLeft ();
         yield return new WaitForSeconds (3); //Change this to however long you want the AI to be looking left for
         LookRight ();
         yield return new WaitForSeconds (3); //Change this to however long you want the AI to be looking right for
         //Return the AI's head rotation to default and start moving again
     }
 
     public void LookLeft(){
         while (transform.rotation != newRotation) {
                         newRotation = Quaternion.LookRotation (targetLeft - transform.position);
                         transform.rotation = Quaternion.Lerp (transform.rotation, newRotation, Time.deltaTime);
                         Debug.Log ("Looking Left");
                 }
     }
 
     public void LookRight(){
         newRotation = Quaternion.LookRotation(targetRight - transform.position);
         while(transform.rotation != newRotation)
         {
             transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, Time.deltaTime);
             Debug.Log("Looking Right");
         }
     }
Comment

People who like this

0 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

19 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

Related Questions

Unity Rotate Raycast on Quaternion 1 Answer

How to get an enemy to face me on a spherical word 0 Answers

Quaternion.Euler rotations don't add up 0 Answers

Rotating direction vectors 1 Answer

Need help with rotation 4 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