60 lines
1.6 KiB
C#
60 lines
1.6 KiB
C#
using UnityEngine;
|
|
|
|
namespace Code.Base
|
|
{
|
|
public class Agent : MonoBehaviour
|
|
{
|
|
public float maxSpeed;
|
|
public float maxAcceleration;
|
|
public float orientation;
|
|
public float rotation;
|
|
public Vector3 velocity;
|
|
protected Steering Steering;
|
|
|
|
void Start()
|
|
{
|
|
velocity = Vector3.zero;
|
|
Steering = new Steering();
|
|
}
|
|
|
|
public void SetSteering(Steering steering) => Steering = steering;
|
|
|
|
public virtual void Update()
|
|
{
|
|
var displacement = velocity * Time.deltaTime;
|
|
orientation += rotation * Time.deltaTime;
|
|
|
|
switch (orientation)
|
|
{
|
|
case < 0:
|
|
orientation += 360;
|
|
break;
|
|
case > 360:
|
|
orientation -= 360;
|
|
break;
|
|
}
|
|
|
|
transform.Translate(displacement, Space.World);
|
|
transform.rotation = new Quaternion();
|
|
transform.Rotate(Vector3.up, orientation);
|
|
}
|
|
|
|
public virtual void LateUpdate()
|
|
{
|
|
velocity += Steering.Linear * Time.deltaTime;
|
|
rotation += Steering.Angular * Time.deltaTime;
|
|
|
|
if (velocity.magnitude > maxSpeed)
|
|
{
|
|
velocity.Normalize();
|
|
velocity *= maxSpeed;
|
|
}
|
|
|
|
if (Steering.Angular == 0) rotation = 0;
|
|
|
|
if (Steering.Linear.sqrMagnitude == 0) velocity = Vector3.zero;
|
|
|
|
Steering = new Steering();
|
|
}
|
|
}
|
|
} |