Files
LowPolyRPG/Assets/Plugins/RaycastPro/_Demo/_Scripts/SteeringController.cs

52 lines
1.7 KiB
C#
Raw Normal View History

2025-06-25 11:10:11 +01:00
using RaycastPro.Detectors;
namespace Plugins.RaycastPro.Demo.Scripts
{
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class SteeringController : MonoBehaviour
{
private Rigidbody rb;
public SteeringDetector detector;
public float accelerationSpeed = 4;
public float arriveDistance = 2f;
public float turnRate = 15;
[SerializeField] private bool movable = true;
[SerializeField] private bool rotatable = true;
[SerializeField] private bool fixRotate = true;
[Range(2f, 50)] public float speed = 10;
private void Start()
{
rb = GetComponent<Rigidbody>();
}
public void Update()
{
if (movable)
{
var inArriveDistance = Vector3.Distance(transform.position, detector.destination.position) < arriveDistance;
rb.linearVelocity = Vector3.Lerp(rb.linearVelocity, inArriveDistance ? Vector3.zero : detector.SteeringDirection * speed,
1 - Mathf.Exp(-accelerationSpeed * Time.deltaTime));
}
if (rotatable)
{
if (fixRotate)
{
transform.forward = Vector3.ProjectOnPlane(detector.SteeringDirection, Vector3.up);
}
else
{
rb.rotation = Quaternion.Lerp(rb.rotation,
Quaternion.LookRotation(detector.SteeringDirection == Vector3.zero ? transform.forward : detector.SteeringDirection, Vector3.up), 1 - Mathf.Exp(-turnRate * Time.deltaTime));
}
}
}
}
}