Files
ID/Assets/Code/Lab3/Align.cs
T
2026-02-10 12:35:10 +03:00

42 lines
1.4 KiB
C#

using Code.Base;
using UnityEngine;
namespace Code.Lab3
{
public class Align : AgentBehaviour
{
public float targetRadius;
public float slowRadius;
public float timeToTarget = 0.1f;
public override Steering GetSteering()
{
var steering = new Steering();
var targetOrientation = target.GetComponent<Agent>().orientation;
var rotation = targetOrientation - Agent.orientation;
rotation = MapToRange(rotation);
var 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;
var angularAccel = Mathf.Abs(steering.Angular);
if (!(angularAccel > Agent.maxAngularAcceleration)) return steering;
steering.Angular /= angularAccel;
steering.Angular *= Agent.maxAngularAcceleration;
return steering;
}
}
}