using System.Collections;
using UnityEngine;
[RequireComponent(typeof(Animator))]
public class Test : MonoBehaviour
{
public Transform lookAtTarget;
public AnimationCurve curve = AnimationCurve.EaseInOut(0.0f, 0.0f, 1.0f, 1.0f);
public float duration;
public float duration1;
private float t;
private float t1;
private Quaternion rot;
private void Start()
{
t = 0;
t1 = 0;
rot = transform.rotation;
}
void LateUpdate()
{
Vector3 forward = transform.TransformDirection(Vector3.forward);
Vector3 toOther = lookAtTarget.transform.position - transform.position;
if (Vector3.Dot(forward, toOther) < 0)
{
t1 += Time.deltaTime;
float s = t1 / duration1;
transform.rotation = Quaternion.Lerp(transform.rotation, rot, curve.Evaluate(s));
}
else
{
t1 = 0;
t += Time.deltaTime;
float s = t / duration;
var targetRotation = Quaternion.LookRotation(lookAtTarget.transform.position - transform.position);
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, curve.Evaluate(s));
}
}
}
In the if part the transform is like "jumping" at once to the original rotation and there make some lerping or not doing at all and I want that it will lerp smooth slowly from it's current rotation back to the original rotation(rot).
Same effect when turret is cool down when there is no target so the turret smoothly return to his original rotation.
I tried also :
transform.rotation = Quaternion.Lerp(transform.rotation, rot, s);
But the same problem remains.
Tried this :
using System.Collections;
using UnityEngine;
[RequireComponent(typeof(Animator))]
public class Test : MonoBehaviour
{
public Transform lookAtTarget;
public AnimationCurve curve = AnimationCurve.EaseInOut(0.0f, 0.0f, 1.0f, 1.0f);
public float duration;
private float t;
private void Start()
{
t = 0;
}
void LateUpdate()
{
t += Time.deltaTime;
float s = t / duration;
Vector3 forward = transform.TransformDirection(Vector3.forward);
Vector3 toOther = lookAtTarget.transform.position - transform.position;
if (Vector3.Dot(forward, toOther) < 0)
{
transform.rotation = Quaternion.Lerp(, transform.rotation, curve.Evaluate(s));
}
else
{
var targetRotation = Quaternion.LookRotation(lookAtTarget.transform.position - transform.position);
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, curve.Evaluate(s));
}
}
}
Update :
my player object name is kid_from_space this screenshot show the player settings in the inspector :
The player have a Rigidbody and also animtor components too.
That is why the script use LateUpdate instead Update.
The script is attached to the player head child object :
This object have the script that make object rotate around the player :
This screenshot is of the child of the object Rotate Around the Target is the basketball in the screenshot the basketball is rotating aorund the player :
This is the script that is attached to the player head :
using System.Collections;
using UnityEngine;
public class LookAtTarget : MonoBehaviour
{
public Transform target;
// Keep this low to make the tracking tight for small target movements.
public float lookChangeDuration = 0.1f;
// Use this to limit our top rotation speed when making a big turn.
// It's measured in "approximately" radians per second.
public float maxLookSpeed = 5f;
Vector3 lookVectorVelocity;
Vector3 originalLookDirection;
private void Start()
{
originalLookDirection = transform.forward;
}
void LateUpdate()
{
Vector3 desiredLook = (target.position - transform.position).normalized;
if (Vector3.Dot(desiredLook, transform.forward) < 0)
{
desiredLook = originalLookDirection;
}
var look = Vector3.SmoothDamp(transform.forward,
desiredLook,
ref lookVectorVelocity,
lookChangeDuration,
maxLookSpeed);
transform.rotation = Quaternion.LookRotation(look);
}
}
This script is attached to the Rotate Around object :
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
[ExecuteAlways]
public class SimpleRotate : MonoBehaviour
{
public List<TextMeshProUGUI> uiText = new List<TextMeshProUGUI>();
public GameObject objToRotateAround;
[Header("The axis by which it will rotate around")]
public Vector3 axis;
[Header("Angle covered per update")]
public float angle;
public float upperLimit, lowerLimit, delay;
private float height, prevHeight, time;
// Update is called once per frame
void Update()
{
transform.RotateAround(objToRotateAround.transform.position, axis.normalized, angle);
time += Time.deltaTime;
if (time > delay)
{
prevHeight = height;
height = Random.Range(lowerLimit, upperLimit);
time = 0;
}
transform.position = new Vector3(transform.position.x, Mathf.Lerp(prevHeight, height, time), transform.position.z);
}
}
The result is that the player head is not rotating at all not looking at the target at all. The player head stay all the time in natural original rotation view.
I found that if I disable the Animator that attached to the player the head will keep rotating nonstop 360 degrees looking at the target. With the Animator turned on enabled true the head is not rotating at all.
This is the player animator controller settings. Grounded is a blend tree :
This screenshot is of the Grounded blend tree the top animation is the HumanoidIdle and this is also the default animation play when running the game :