FX , VO, fixy
This commit is contained in:
36
Assets/Scripts/PlayerAutoRotator.cs
Normal file
36
Assets/Scripts/PlayerAutoRotator.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Beyond
|
||||
{
|
||||
public class PlayerAutoRotator : MonoBehaviour
|
||||
{
|
||||
public float rotationSpeed = 5f;
|
||||
private Transform targetToLook;
|
||||
private bool shouldRotate = false;
|
||||
|
||||
public void LookAtTarget(Transform target)
|
||||
{
|
||||
targetToLook = target;
|
||||
shouldRotate = true;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (!shouldRotate || targetToLook == null)
|
||||
return;
|
||||
|
||||
Vector3 direction = (targetToLook.position - transform.position).normalized;
|
||||
direction.y = 0;
|
||||
|
||||
if (direction == Vector3.zero) return;
|
||||
|
||||
Quaternion targetRotation = Quaternion.LookRotation(direction);
|
||||
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * rotationSpeed);
|
||||
|
||||
if (Quaternion.Angle(transform.rotation, targetRotation) < 1f)
|
||||
{
|
||||
shouldRotate = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user