Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

transform.LookAt smooth transition instead of instant snapping?

Discussion in 'Scripting' started by MSachs, Mar 16, 2018.

  1. MSachs

    MSachs

    Joined:
    Nov 22, 2017
    Posts:
    122
    Hi,

    this might be a rather strange question to ask but I have an object which should always LookAt the camera. Currently I am using a transform.LookAt script which restricts the rotation to the y axis so it always stays flat on the ground.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class LookAtCamMaschine : MonoBehaviour
    6. {
    7.     public GameObject target;
    8.  
    9.  
    10.     void Update ()
    11.     {
    12.         Vector3 targetPosition = new Vector3 (target.transform.position.x, transform.position.y, target.transform.position.z);
    13.         transform.LookAt(targetPosition);
    14.     }
    15. }

    Now there are different occasions where I am disabling this script so the object doesnt LookAt the camera anymore. When I then move the camera while the script is disabled and reactivate the script the objects instantly snap in the right position to LookAt the camera again.

    Now my question: Is there a way to have this transition smoothly after reactivating the script?

    Thanks in advance :)
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    MSachs likes this.
  3. MSachs

    MSachs

    Joined:
    Nov 22, 2017
    Posts:
    122
  4. MSachs

    MSachs

    Joined:
    Nov 22, 2017
    Posts:
    122
    Thanks! I pretty much have the effect I wanted now but how would I go about slowing the rotation down towards the end? right now it always turns with the same speed until it comes to a sudden stop...

    Code (CSharp):
    1.     public Transform target;
    2.     public float speed;
    3.  
    4.     void Update ()
    5.     {
    6.      
    7.         Vector3 targetDir = target.position - transform.position;
    8.         targetDir.y = 0;
    9.         float step = speed * Time.deltaTime;
    10.  
    11.         Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, step, 0.0F);
    12.         Debug.DrawRay(transform.position, newDir, Color.red);
    13.  
    14.         transform.rotation = Quaternion.LookRotation(newDir);
    15.     }
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Sorry, I didn't see your response until a few minutes ago for some reason.

    I'm not actually sure about that, actually. I did a quick search but wasn't sure about the answers.. maybe someone else will have some input. :)
     
    MSachs likes this.
  6. MSachs

    MSachs

    Joined:
    Nov 22, 2017
    Posts:
    122
    No problem! Thanks anyway :)
     
  7. MSachs

    MSachs

    Joined:
    Nov 22, 2017
    Posts:
    122
    Got it working :D

    I have no idea if this is the best way to do this but at least it works as I want it to :)

    Code (CSharp):
    1. public Transform target;
    2. public float damping;
    3.  
    4. void LateUpdate ()
    5. {
    6.   var rotation = Quaternion.LookRotation (target.position - transform.position);
    7.   // rotation.x = 0; This is for limiting the rotation to the y axis. I needed this for my project so just
    8.   // rotation.z = 0;                 delete or add the lines you need to have it behave the way you want.
    9.   transform.rotation = Quaternion.Slerp (transform.rotation, rotation, Time.deltaTime * damping);
    10. }
     
  8. maxbattle84

    maxbattle84

    Joined:
    Sep 19, 2018
    Posts:
    1
    Hi,
    I encontered a similar problem, but I haven't figured it out yet.
    So, in Unity 5.6.4f1 I used this piece of code and the result was good, the character turn his head towards the destination smoothly:

    Code (CSharp):
    1. void OnAnimatorIK()
    2.    {
    3.       if (mDestination == null)
    4.          Weight = 0;
    5.       if (mBlendCurrentAmount > Weight || mDestination == null)
    6.       {
    7.             mBlendCurrentAmount -= BlendingTime * Time.deltaTime;BlendingTime));
    8.             if (mBlendCurrentAmount < Weight)
    9.                 mBlendCurrentAmount = Weight;
    10.       }
    11.       else if(mBlendCurrentAmount < Weight)
    12.       {
    13.             mBlendCurrentAmount += BlendingTime * Time.deltaTime;
    14.             if (mBlendCurrentAmount > Weight)
    15.                 mBlendCurrentAmount = Weight;
    16.         }
    17.         animator.SetLookAtWeight((float)mBlendCurrentAmount, 0.00f, 1.00f, 1.00f, 1.00f);
    18.      
    19.         if (mDestination != null)
    20.         {
    21.             animator.SetLookAtPosition(mDestination.position);
    22.         }
    23.    }
    Instead in Unity 2018.3.7f1 when I switch off the "look_at" function the transition isn't smooth and the character turn his head with a snap.

    Someone could help me?

    Thanks a lot.
     
  9. G-Locasso

    G-Locasso

    Joined:
    Aug 28, 2018
    Posts:
    5
    Amazing! The most simplest, and yet right to the point solution. Works great :)
     
  10. JohnnyCar

    JohnnyCar

    Joined:
    Aug 5, 2018
    Posts:
    9
    I will try thank you!
     
  11. JorisBohnson

    JorisBohnson

    Joined:
    Mar 10, 2022
    Posts:
    1
    I am 4 years late but you are a god