Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 /
This question was closed Nov 30, 2016 at 09:55 PM by DJGhostViper for the following reason:

Other

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

1 Reply

  • Sort: 
avatar image
0

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

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

ienumerator quaternion only rotating once 1 Answer

calculating player rotation direction 0 Answers

How to turn a character frame independently? 1 Answer

Random smooth rotation on z-axis every other second 2 Answers

Boids for 2D 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