53 lines
1.9 KiB
C#
53 lines
1.9 KiB
C#
using UnityEngine;
|
|
|
|
namespace Code.Example.Race.Racer.AI.ID
|
|
{
|
|
public class Agent : MonoBehaviour
|
|
{
|
|
public float maxSpeed;
|
|
public float maxAccel;
|
|
public float orientation;
|
|
public float rotation;
|
|
public Vector3 velocity;
|
|
protected Steering steering;
|
|
|
|
private void Start()
|
|
{
|
|
velocity = Vector3.zero;
|
|
steering = new Steering();
|
|
}
|
|
public virtual void Update ()
|
|
{
|
|
Vector3 displacement = velocity * Time.deltaTime;
|
|
orientation += rotation * Time.deltaTime;
|
|
// необходимо ограничить значение переменной orientation
|
|
// диапазоном (0-360)
|
|
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 virtual void LateUpdate()
|
|
{
|
|
velocity += steering.linear * Time.deltaTime;
|
|
rotation += steering.angular * Time.deltaTime;
|
|
if (velocity.magnitude > maxSpeed)
|
|
{
|
|
velocity = velocity.normalized * maxSpeed; // Исправлено: корректное нормирование вектора
|
|
}
|
|
if (steering.angular == 0.0f)
|
|
{
|
|
rotation = 0.0f;
|
|
}
|
|
if (steering.linear.sqrMagnitude == 0.0f)
|
|
{
|
|
velocity = Vector3.zero;
|
|
}
|
|
// Удалена строка: steering = new Steering();
|
|
// (создание нового объекта каждый кадр приведет к утечке памяти и сбросу данных)
|
|
}
|
|
}
|
|
} |