Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

object to turn slowly towards player

Discussion in 'Scripting' started by Resilo, May 8, 2017.

  1. Resilo

    Resilo

    Joined:
    Dec 8, 2016
    Posts:
    139
    Ok so after looking around already the only method i could find for making an enemy look at a player is the Lookat() function. problem is i need my enemy to slowly rotate to face the player.

    what is the simplest way to achieve this?
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    Use a coroutine.

    Loop every frame slerping from the initial rotation to the Quaternion.LookRotation rotation.

    Code (csharp):
    1.  
    2. IEnumerator LookAtOverTime(Transform t, Transform target, float dur)
    3. {
    4.     Quaternion start = t.rotation;
    5.     Quaternion end = Quaternion.LookRotation(target.position - t.position);
    6.     float t = 0f;
    7.     while(t < dur)
    8.     {
    9.           t.rotation = Quaternion.Slerp(start, end, t / dur);
    10.           yield return null;
    11.           t += Time.deltaTime;
    12.     }
    13. }
    14.  
    Something like that (code untested, written directly in the browser wysiwyg).
     
    soco2211 likes this.
  3. Resilo

    Resilo

    Joined:
    Dec 8, 2016
    Posts:
    139
    there's really no simple function for this?
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    Not built into Unity.

    There are various 3rd party tween engines that could do it in fewer lines of code.

    The Coroutine though is fairly simple.
     
  5. soco2211

    soco2211

    Joined:
    Dec 25, 2016
    Posts:
    7
    I know this post is about a year old, but this solved an issue I was having. Just wanted to point out for other people though that the Transform variable "t" in the parameter list should be different from the float variable "t" inside the Coroutine. I changed the float "t" to "rotationTime" so "rotationTime < dur", "rotationTime / dur", and finally "rotationTime += Time.deltaTime"
     
  6. BackToPanda

    BackToPanda

    Joined:
    Nov 30, 2016
    Posts:
    5
    Thank you. For others possibly as code:

    Code (CSharp):
    1. IEnumerator LookAtOverTime(Transform t, Transform target, float dur)
    2.     {
    3.         Quaternion start = t.rotation;
    4.         Quaternion end = Quaternion.LookRotation(target.position - t.position);
    5.         float rotationTime = 0f;
    6.         while (rotationTime < dur)
    7.         {
    8.             t.rotation = Quaternion.Slerp(start, end, rotationTime / dur);
    9.             yield return null;
    10.             rotationTime += Time.deltaTime;
    11.         }
    12.     }
     
  7. diliupg

    diliupg

    Joined:
    Jan 23, 2018
    Posts:
    45
    This is an easy way to do this.
    Code (CSharp):
    1.     private void TurnTowardsTargetToShoot(int turnSpeed)
    2.     {
    3.         worldAimTarget = mouseWorldPosition;
    4.         worldAimTarget.y = transform.position.y;
    5.         aimDirection = (worldAimTarget - transform.position).normalized;
    6.         // turn fast if shooting pistol
    7.         transform.forward = Vector3.Lerp(transform.forward, aimDirection, Time.deltaTime * turnSpeed);
    8.     }
    9.