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! 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…
I have no idea if this is the best way to do this but at least it works as I want it to
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);
}
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: