Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 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
Add comment · 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

· Add your reply
  • Sort: 
avatar image
0

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
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

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

Rotate floor plane in-game via C# script 1 Answer

Problem with normal snapping and rotations. 1 Answer

Rotate vector towards another vector around perpendicular vector 1 Answer

Spacesim AI pilot, vector3.cross issues 1 Answer

How do you rotate an object with Vector that has another vector defining its angle 0 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