transform.LookAt smooth transition instead of instant snapping?

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.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LookAtCamMaschine : MonoBehaviour
{
    public GameObject target;


    void Update ()
    {
        Vector3 targetPosition = new Vector3 (target.transform.position.x, transform.position.y, target.transform.position.z);
        transform.LookAt(targetPosition);
    }
}

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

Something like this from yesterday on the forums? Locking the x and z axis while using transform.LookAt()?

oh my goodness, lol, that was you… So, try using one of the Quaternion examples given there, and then you can Rotate Towards, for example.

1 Like

haha yeah :smile: one problem building on another one :wink:

Thanks I will try that :slight_smile:

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…

    public Transform target;
    public float speed;

    void Update ()
    {
     
        Vector3 targetDir = target.position - transform.position;
        targetDir.y = 0;
        float step = speed * Time.deltaTime;

        Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, step, 0.0F);
        Debug.DrawRay(transform.position, newDir, Color.red);

        transform.rotation = Quaternion.LookRotation(newDir);
    }

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. :slight_smile:

1 Like

No problem! Thanks anyway :slight_smile:

Got it working :smile:

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

public Transform target;
public float damping;

void LateUpdate ()
{
  var rotation = Quaternion.LookRotation (target.position - transform.position);
  // rotation.x = 0; This is for limiting the rotation to the y axis. I needed this for my project so just
  // rotation.z = 0;                 delete or add the lines you need to have it behave the way you want.
  transform.rotation = Quaternion.Slerp (transform.rotation, rotation, Time.deltaTime * damping);
}
4 Likes

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:

void OnAnimatorIK()
   {
      if (mDestination == null)
         Weight = 0;
      if (mBlendCurrentAmount > Weight || mDestination == null)
      {
            mBlendCurrentAmount -= BlendingTime * Time.deltaTime;BlendingTime));
            if (mBlendCurrentAmount < Weight)
                mBlendCurrentAmount = Weight;
      }
      else if(mBlendCurrentAmount < Weight)
      {
            mBlendCurrentAmount += BlendingTime * Time.deltaTime;
            if (mBlendCurrentAmount > Weight)
                mBlendCurrentAmount = Weight;
        }
        animator.SetLookAtWeight((float)mBlendCurrentAmount, 0.00f, 1.00f, 1.00f, 1.00f);
     
        if (mDestination != null)
        {
            animator.SetLookAtPosition(mDestination.position);
        }
   }

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.

Amazing! The most simplest, and yet right to the point solution. Works great :slight_smile:

I will try thank you!

I am 4 years late but you are a god