ID3/Assets/Align.cs
2026-03-19 02:02:28 +03:00

45 lines
1.4 KiB
C#

using UnityEngine;
using System.Collections;
public class Align : AgentBehaviour
{
public float targetRadius;
public float slowRadius;
public float timeToTarget = 0.1f;
public override Steering GetSteering()
{
Steering steering = new Steering();
float targetOrientation = target.GetComponent<Agent>().orientation;
float rotation = targetOrientation - agent.orientation;
rotation = MapToRange(rotation);
float rotationSize = Mathf.Abs(rotation);
if (rotationSize < targetRadius)
return steering;
float targetRotation;
if (rotationSize > slowRadius)
targetRotation = agent.maxRotation;
else
targetRotation = agent.maxRotation * rotationSize / slowRadius;
targetRotation *= rotation / rotationSize;
steering.angular = targetRotation - agent.rotation;
steering.angular /= timeToTarget;
float angularAccel = Mathf.Abs(steering.angular);
if (angularAccel > agent.maxAngularAccel)
{
steering.angular /= angularAccel;
steering.angular *= agent.maxAngularAccel;
}
return steering;
}
protected float MapToRange(float rotation)
{
while (rotation < -180.0f)
rotation += 360.0f;
while (rotation > 180.0f)
rotation -= 360.0f;
return rotation;
}
}