68 lines
1.9 KiB
C#
68 lines
1.9 KiB
C#
using UnityEngine;
|
|
|
|
namespace Code.Base
|
|
{
|
|
public class Agent : MonoBehaviour
|
|
{
|
|
public float maxSpeed;
|
|
public float maxAcceleration;
|
|
public float maxRotation;
|
|
public float maxAngularAcceleration;
|
|
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) => SetSteering(steering, 1.0f);
|
|
|
|
public void SetSteering(Steering steering, float weight)
|
|
{
|
|
Steering.Linear += weight * steering.Linear;
|
|
Steering.Angular += weight * steering.Angular;
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|
|
} |