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
1
Question by ZeFirestarter · Apr 13, 2015 at 09:51 PM · transform.lookat

transform.LookAt() function isn't working properly

First of all, greetings Unity community. This is my first post here. I'm a total beginner. I tried making a showroom, adapting the scripts from the "Stealth Project Tutorial" + what I could gather from the Documentation.

I want to make an Art-Gallery, where NPCs walk around staring at pictures. The NPCs walk through the assigned wayPoints. When they hit the picture collider, they stay for a couple of seconds looking at the picture. I've got everything working so far, except for them looking at the different pictures.

I've tried different solutions suggested here (unityAnswers), but so far nothing has worked.

I'd appreciate any help. Here is my code so far (you can ignore the Patrolling() function, since it was taken from the Stealth Project and should be correct):

Art-Gallery

photo1 photo2

     public class NPCAI : MonoBehaviour 
     {
         public bool pictureInSight;
         public int StartAdmiration;
         public int StopAdmiration;
         public float angleBetween = 0.5f;
         public float patrolSpeed = 2f;
         public float patrolWaitTime = 1f;
         public Transform[] patrolWayPoints;
             //I've tried it with Transform and Gameobject, neither works
         public Transform trans4;
         public GameObject target1;
         public GameObject target2;
         public GameObject target3;
         public GameObject target4;
                 
         private NavMeshAgent nav;
         private float patrolTimer;
         private int wayPointIndex;
         private int targetIndex;
         
         void Awake()
         {
             nav = GetComponent<NavMeshAgent>();
         }
     
         void Update()
         {
             if (pictureInSight == false) 
             {
                 Patrolling();
             } 
         }
     
     
         void Patrolling()
         {
             nav.speed = patrolSpeed;
             
             if(nav.remainingDistance < nav.stoppingDistance)
             {
                 patrolTimer += Time.deltaTime;
                 
                 if(patrolTimer >= patrolWaitTime)
                 {
                     // ... increment the wayPointIndex.
                     if(wayPointIndex == patrolWayPoints.Length - 1)
                         wayPointIndex = 0;
                     else
                         wayPointIndex++;
                     
                     // Reset the timer.
                     patrolTimer = 0;
                 }
             }
             else
                 patrolTimer = 0;
             
             nav.destination = patrolWayPoints[wayPointIndex].position;
         }
     
         void OnTriggerEnter (Collider other)
     {
         if (other.gameObject == target1) 
         {
             pictureInSight = true;
 
             Debug.Log ("Collision with target1");
 
             //This was my first try. 
             //The result is, that it rotates my NPC at a weird angle.[watch photo1]
             /*transform.LookAt(trans1);*/
 
             //I found this solution in the Forums. [watch photo2]
             //The result, looks in a random direction, but NPC doesn't have a weird angle anymore
             //if I do the same with x and z it makes no difference
             var targetPos1 = target1.transform.position;
             targetPos1.y = transform.position.y;
             transform.LookAt(targetPos1);
 
             Vector3 destination = target1.transform.position - transform.position;
             angleBetween = Vector3.Angle(transform.forward, destination);
 
             int admirationTime = (Random.Range(StartAdmiration, StopAdmiration));
             Debug.Log ("Waiting time is");
             Debug.Log(admirationTime);
     
             Invoke("ResetValues", admirationTime);
                 }
 
         if (other.gameObject == target2) 
         {
             pictureInSight = true;
             
             Debug.Log ("Collision with picture");
             
             var targetPos2 = target2.transform.position;
             targetPos2.y = transform.position.y;
             targetPos2.z = transform.position.z;
             transform.LookAt(targetPos2);
             
             Vector3 destination = target2.transform.position - transform.position;
             angleBetween = Vector3.Angle(transform.forward, destination);
             
             int admirationTime = (Random.Range(StartAdmiration, StopAdmiration));
             Debug.Log ("Waiting time is");
             Debug.Log(admirationTime);
 
             Invoke("ResetValues", admirationTime);
         }
 
         if (other.gameObject == target3) 
         {
             pictureInSight = true;
             
             Debug.Log ("Collision with picture");
             
             transform.LookAt(target3.transform.position);
             
             Vector3 destination = target3.transform.position - transform.position;
             angleBetween = Vector3.Angle(transform.forward, destination);
             
             int admirationTime = (Random.Range(StartAdmiration, StopAdmiration));
             Debug.Log ("Waiting time is");
             Debug.Log(admirationTime);
             
             Invoke("ResetValues", admirationTime);
         }
 
         if (other.gameObject == target4) 
         {
             pictureInSight = true;
             
             Debug.Log ("Collision with target4");
             
             transform.LookAt(new Vector3(trans4.position.x, transform.position.y, trans4.position.z));
             
             Vector3 destination = target4.transform.position - transform.position;
             angleBetween = Vector3.Angle(transform.forward, destination);
             
             int admirationTime = (Random.Range(StartAdmiration, StopAdmiration));
             Debug.Log ("Waiting time is");
             Debug.Log(admirationTime);
             
             Invoke("ResetValues", admirationTime);
         }
     }
     
     void ResetValues()
     {
             pictureInSight = false;
         Patrolling ();
         //transform.LookAt (Vector3.zero);
     }
     }

PS: I know I should make arrays instead of drag&drop each target, but I tried and failed miserably. Thank you in advance. Best Regards.

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

Answer by ZeFirestarter · Apr 14, 2015 at 05:27 PM

Since I'm a retarded mofo, I just realized that the Animation script was conflicting with this NPC-AI script. Both scripts are on the NPCs. The "nav.updaterotation" was set to false, I changed it to true and now it doesn't tilt my NPCs anymore. But the NPCs were still looking in a random direction instead of the object they should be looking at. After that I tried it with Quaternion and giving the Vector3 like Cherno suggested and it worked.

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
2

Answer by Cherno · Apr 13, 2015 at 10:46 PM

It's possible that the pivot of the model doesn't conform to Unity's standard and hence while technically the LookAt function works right, effectively the models rotates the wrong way from a realism standpoint. Other than that, there's always the possibility to set one or more of the axes to zero immediately after calling LookAt:

 transform.LookAt(trans1);
 transform.rotation.eulerAngles = new Vector3(0f, transform.rotation.eulerAngles.y, 0f);

Note that the Scripting API for Transform.LooAt also states that the function accepts a Vector3 parameter "worldUp", so you can try passing it Vector3.up and see if that helps.

As for using arrays and collections:

Link

Comment
Add comment · Show 4 · 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 ZeFirestarter · Apr 14, 2015 at 07:41 AM 0
Share

Thank you for your help. I tried what you said with the eulerAngles, but it gives me the following error:

error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.rotation'. Consider storing the value in a temporary variable

I already tried the Vector3.up, but it has no effect. I took the model and Animator from the S$$anonymous$$lth-Project, after what you said I tried with my own models. Still the same problem.

Thank you for your time and the links. I'll definitely check the arrays and collections wiki later.

avatar image Cherno · Apr 14, 2015 at 08:15 AM 1
Share

Try this ins$$anonymous$$d:

 transform.LookAt(trans1);
 Quaternion q = transform.rotation;
 q.eulerAngles = new Vector3(0f, transform.rotation.eulerAngles.y, 0f);
 transform.rotation = q;
 

Also try Vector3.right, Vector3.forward, -Vector3.right etc.

avatar image ZeFirestarter · Apr 14, 2015 at 12:03 PM 0
Share

Thank you very much Cherno. You got me thinking that I had to search for the problem outside of the script. I found out that the other script which animates the NPCs was conflicting with it.

avatar image SohailBukhari · Mar 31, 2021 at 04:46 PM 0
Share

I know this is old but this answer will help the community.

Unity transform.LookAt() not updating with a moving target

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Player exits car, need proper rotation - syntax help. 1 Answer

How to lock axis for LookAt? 1 Answer

How do I add force to the direction a game object is facing? 2 Answers

Screen Edge GameObject Position Pointer 0 Answers

getting out of LookAt messes up first person view 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