• 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 /
This question was closed Nov 30, 2016 at 09:55 PM by DJGhostViper for the following reason:

Other

avatar image
Question by DJGhostViper · Nov 30, 2016 at 03:29 AM · rotationaiquaternioncoroutineienumerator

How can you tell if a gameobject is looking directly at another gameobject?

I'm using a coroutine to make a troop look at the enemy troop when it is within a radius, I want the troop to look at the gameobject. Is there a way to tell if the gameobject is looking directly at the other gameobject so I can stop the current coroutine and move to the next coroutine?

 public IEnumerator LookAtEnemy()
     {
 
         Debug.Log ("Started");
 
         yield return new WaitForSeconds (1);
 
         //while (//Repeat until the object is staring at the object...) {
         
 
             if (this.gameObject.CompareTag ("ShootingFriendly") && SightS.EnemiesInRange.Count > 0) {
                 {
                     Debug.Log ("Turning");
                     GameObject CurrentEnemy = SightS.EnemiesInRange [0].gameObject;
                     Quaternion Rot1 = Quaternion.LookRotation (CurrentEnemy.transform.position - this.gameObject.transform.position);
                     this.gameObject.transform.rotation = Quaternion.Slerp (this.gameObject.transform.rotation, Rot1, Time.deltaTime * 3);
 
 
                     Quaternion Rot = Quaternion.LookRotation (CurrentEnemy.transform.position - Gun.transform.position);
                     Gun.transform.rotation = Quaternion.Slerp (Gun.transform.rotation, Rot, Time.deltaTime * 3);
                 }
 
 
             //}
 
 
         }
 
     }
Comment

People who like this

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

  • Sort: 
avatar image

Answer by Bunny83 · Nov 30, 2016 at 04:26 AM

Since you have already the target rotation as quaternion you can simply take the angle between them and test if it's blow some threshold.

 if (Quaternion.Angle(Gun.transform.rotation, Rot) < 5f)
 {
     // gun almost looks at the target
 }

You can make the threshold smaller, but keep in mind it might take quite a bit longer. Mathematically you will never directly look at the target since you always only rotate a fraction of the remaining difference. You will get closer and closer but it takes longer and longer. Due to floating point precision limitations you might actually reach the target but it can take a very long time. For most applications a small threshold would be enough.

If you want it more precise you shouldn't use Slerp with Time.deltaTime. You could use RotateTowards or similar methods.

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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

100 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

How to rotate an object around another 60 degrees with a keypress? 0 Answers

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

Rotating an object around 2 axis at the same time 0 Answers

Basic AI Locked Axis 1 Answer

How to get an enemy to face me on a spherical word 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