The goal is to make smooth rotating look at.
The parent object is my character have a Rigidbody.
The script is attached to the character parent. I make a reference to the character child name head.
I have a cube as the target the cube is rotating around the character with random height.
The character is never looking at the cube. I tried to change from LateUpdate to Update I tried to add a child of the head empty gameobject and reference the empty gameobject but still the character head is not rotating to the target.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SmoothLookAt : MonoBehaviour
{
public Transform leadObj;
public Transform lookAtTarget;
public float damping = 6;
public bool smooth = true;
private void LateUpdate()
{
if (lookAtTarget)
{
if (smooth)
{
// Look at and dampen the rotation
var rotation = Quaternion.LookRotation(lookAtTarget.position - leadObj.position);
leadObj.rotation = Quaternion.Slerp(leadObj.rotation, rotation, Time.deltaTime * damping);
}
else
{
// Just lookat
leadObj.LookAt(lookAtTarget);
}
}
}
private void Start()
{
// Make the rigid body not change rotation
if (leadObj.GetComponent<Rigidbody>() != null)
leadObj.GetComponent<Rigidbody>().freezeRotation = true;
}
}
It seems to be working if I reference the head directly as leadObj and inside LateUpdate the problem is that I have to set the damping value to 260.
Why I need to set the damping value to be so high ?
And how can I make that the head will rotate to the target with automatic speed depending on the target moving speed ? So if I don't set any damping value for example 0 or null it will automatic calculate the needing speed to rotate facing the target ?
I see now another problem. If I set the damping value to 100 for example the character head is stuttering the head will look at the target but will stuttering. I want that if the damping value is 100 or lower than rotate the leadObj with a delay facing the target. Something like when a turret is rotating facing a missile and if the missile is moving too fast the turret is rotating with some delay because he rotate slower. How can I do it ?
This is working using Lerp. But why if I set for example the duration value to 0 it's not affecting the speed of the leadObj rotation ?
The leadObj keep rotating same speed facing the target even if the duration is 0.
What am I missing ?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SmoothLookAt : MonoBehaviour
{
public Transform leadObj;
public Transform lookAtTarget;
public float duration;
public bool smooth = true;
private float t = 0.0f;
private void Start()
{
// Make the rigid body not change rotation
if (leadObj.GetComponent<Rigidbody>() != null)
leadObj.GetComponent<Rigidbody>().freezeRotation = true;
}
private void LateUpdate()
{
if (lookAtTarget)
{
if (smooth)
{
t += Time.deltaTime;
float s = t / duration;
// Look at and dampen the rotation
var rotation = Quaternion.LookRotation(lookAtTarget.position - leadObj.position);
leadObj.rotation = Quaternion.Lerp(leadObj.rotation, rotation, s);
}
else
{
// Just lookat
leadObj.LookAt(lookAtTarget);
}
}
}
}