• 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 PlasticFrames · Jun 29, 2021 at 02:24 PM · transform.positionlookatrotatetowardsquaternion.lookrotationquaternion.slerp

Smooth LookAt OnTriggerEnter for static enemies

Hi,

I've got a very simple setup where enemies are continually rotating until the player enters their range, at which point they need to turn towards the player. I've tried numerous Quaternion rotations and Quaternion.Lerps but the rotation is either instantaneous, in the wrong direction or throws the enemies off their axis entirely.

Any help would be appreciated!

 public GameObject player;

 [SerializeField] public Vector3 yAngle;

 public bool isTriggered = false;

 // Start is called before the first frame update
 void Start()
 {
     switchScript = GetComponent<EnemySwitch>();
     player = GameObject.FindWithTag("Player");
 }

 // Update is called once per frame
 void Update()
 {
     if (!isTriggered)
     {
         transform.Rotate(yAngle, Space.Self);
     }
     
     if (isTriggered)
     {
         transform.LookAt(player.transform.position);
     }  
 }

 void OnTriggerEnter(Collider other) 
 {
     if (other.gameObject.CompareTag("Player"))
     {
         isTriggered = true;
     }    
 }
 
 void OnTriggerExit(Collider other) 
 {
     if (other.gameObject.CompareTag("Player"))
     {
         isTriggered = false;
     }    
 }
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

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Hellium · Jun 29, 2021 at 03:51 PM

 [SerializeField] private Vector3 yAngle;
 [SerializeField] private float targetLookAtRotationSpeed;
 private Transform target;

 // Update is called once per frame
 void Update()
 {
     if (target != null)
     {
         Vector3 forward = target.position - transform.position;
         forward.y = 0; // Optional but will prevent the object to look up or down
         transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.LookRotation(forward), targetLookAtRotationSpeed * Time.deltaTime);
     }
     else
     {
         transform.Rotate(yAngle * Time.deltaTime);
     }
 }
 void OnTriggerEnter(Collider other)
 {
     if (other.gameObject.CompareTag("Player"))
     {
         target = other.transform;
     }
 }

 void OnTriggerExit(Collider other)
 {
     if (other.gameObject.CompareTag("Player"))
     {
         target = null;
     }
 }
Comment
Add comment · Show 1 · 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 PlasticFrames · Jun 30, 2021 at 09:17 AM 0
Share

Brilliant, super simple solution! Thank you.

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

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

Rotate slowly to target position? 3 Answers

How do I make an object in game face the direction it's traveling while viewing from above? 1 Answer

Quaternion Slerp 180 Away from target 1 Answer

How to make character rotate in the direction of movement and direction of the camera? 1 Answer

Rotating gameObject to aim at target using Quaternion 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