44 lines
1.3 KiB
C#
44 lines
1.3 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class PlayerHeadSmoothFollow : MonoBehaviour
|
|
{
|
|
private Transform target;
|
|
public float positionLerpSpeed = 0.1f;
|
|
public float rotationLerpSpeed = 0.1f;
|
|
public bool shouldUpdateRotation = true;
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
StartCoroutine(AfterGameLoaded());
|
|
}
|
|
|
|
IEnumerator AfterGameLoaded()
|
|
{
|
|
yield return new WaitUntil(() => !LoadManager.GetInstance().LoadingGame);
|
|
target = PlayerManager.GetInstance().playerHead.transform;
|
|
}
|
|
|
|
|
|
// Update is called once per frame
|
|
void FixedUpdate()
|
|
{
|
|
// 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;
|
|
}
|
|
}
|
|
}
|