56 lines
1.4 KiB
C#
56 lines
1.4 KiB
C#
using UnityEngine;
|
|
using System.Collections;
|
|
|
|
public class Agent : MonoBehaviour
|
|
{
|
|
public float maxSpeed;
|
|
public float maxAccel;
|
|
public float maxRotation;
|
|
public float maxAngularAccel;
|
|
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)
|
|
{
|
|
this.steering = steering;
|
|
}
|
|
|
|
public virtual void Update()
|
|
{
|
|
Vector3 displacement = velocity * Time.deltaTime;
|
|
orientation += rotation * Time.deltaTime;
|
|
if (orientation < 0.0f)
|
|
orientation += 360.0f;
|
|
else if (orientation > 360.0f)
|
|
orientation -= 360.0f;
|
|
transform.Translate(displacement, Space.World);
|
|
transform.rotation = new Quaternion();
|
|
transform.Rotate(Vector3.up, orientation);
|
|
}
|
|
|
|
public void LateUpdate()
|
|
{
|
|
velocity += steering.linear * Time.deltaTime;
|
|
rotation += steering.angular * Time.deltaTime;
|
|
if (velocity.magnitude > maxSpeed)
|
|
{
|
|
velocity.Normalize();
|
|
velocity = velocity * maxSpeed;
|
|
}
|
|
|
|
if (steering.linear.sqrMagnitude == 0.0f)
|
|
{
|
|
velocity = Vector3.zero;
|
|
}
|
|
steering = new Steering();
|
|
}
|
|
}
|