using System.Collections; using System.Collections.Generic; using UnityEngine; public class FollowTargetSmooth : MonoBehaviour { [Tooltip("You can leave this field empty if you want the followable to be the transform that this script is attached to.")] public Transform followableObject; public Transform target; public float positionLerpSpeed = 0.1f; public float rotationLerpSpeed = 0.1f; public bool shouldUpdateRotation = true; public bool targetIsPlayer; private void Start() { if (targetIsPlayer) { target = PlayerManager.GetInstance().playerGameObj.transform; } } public void MakePlayerToFollowTarget() { followableObject = PlayerManager.GetInstance().playerGameObj.transform; } public void ChangeTarget(Transform newTarget) { target = newTarget; } public void NoTarget() { target = null; } public void TargetIsPlayer() { targetIsPlayer = true; target = PlayerManager.GetInstance().playerGameObj.transform; } private void FixedUpdate() { if (target != null) { if (followableObject == null) { // Calculate the new position using Lerp Vector3 newPosition = Vector3.Lerp(transform.position, target.position, positionLerpSpeed * Time.fixedDeltaTime); // Move the GameObject to the new position transform.position = newPosition; if (shouldUpdateRotation) { // Calculate the new rotation using Lerp Quaternion newRotation = Quaternion.Lerp(transform.rotation, target.rotation, rotationLerpSpeed * Time.fixedDeltaTime); // Rotate the GameObject to the new rotation transform.rotation = newRotation; } } else { // Calculate the new position using Lerp Vector3 newPosition = Vector3.Lerp(followableObject.position, target.position, positionLerpSpeed * Time.fixedDeltaTime); // Move the GameObject to the new position followableObject.position = newPosition; if (shouldUpdateRotation) { // Calculate the new rotation using Lerp Quaternion newRotation = Quaternion.Lerp(followableObject.rotation, target.rotation, rotationLerpSpeed * Time.fixedDeltaTime); // Rotate the GameObject to the new rotation followableObject.rotation = newRotation; } } } } }