lab2 dop
This commit is contained in:
1 parent
8151450cdc
commit
a70076ebe9
7 files changed
+1619
-2
No files matched your search
@@ -0,0 +1,69 @@
|
||||
using Code.Base;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Code.Lab2
|
||||
{
|
||||
public class ArriveAvoid : AgentBehaviour
|
||||
{
|
||||
public float targetRadius = 5f;
|
||||
public float slowRadius = 10f;
|
||||
public float timeToTarget = 0.1f;
|
||||
|
||||
public Transform obstacle;
|
||||
public float avoidanceRadius = 10f;
|
||||
public float maxAvoidForce = 50f;
|
||||
|
||||
public override Steering GetSteering()
|
||||
{
|
||||
var steering = new Steering();
|
||||
|
||||
var toTarget = target.transform.position - transform.position;
|
||||
var distanceToTarget = toTarget.magnitude;
|
||||
|
||||
if (distanceToTarget < targetRadius)
|
||||
return steering;
|
||||
|
||||
var targetSpeed = distanceToTarget > slowRadius
|
||||
? Agent.maxSpeed
|
||||
: Agent.maxSpeed * (distanceToTarget / slowRadius);
|
||||
|
||||
var desiredVelocity = toTarget.normalized * targetSpeed;
|
||||
var arriveForce = (desiredVelocity - Agent.velocity) / timeToTarget;
|
||||
|
||||
var avoidForce = Vector3.zero;
|
||||
|
||||
if (obstacle)
|
||||
{
|
||||
var toObstacle = obstacle.position - transform.position;
|
||||
var distToObstacle = toObstacle.magnitude;
|
||||
|
||||
if (distToObstacle < avoidanceRadius)
|
||||
{
|
||||
var forward = Agent.velocity.normalized;
|
||||
if (forward == Vector3.zero) forward = toTarget.normalized;
|
||||
|
||||
var awayFromObstacle = -toObstacle;
|
||||
|
||||
var lateral = awayFromObstacle - Vector3.Project(awayFromObstacle, forward);
|
||||
|
||||
if (lateral.magnitude < 0.1f)
|
||||
{
|
||||
lateral = new Vector3(-forward.z, 0f, forward.x).normalized;
|
||||
}
|
||||
|
||||
var avoidanceStrength = 1f - (distToObstacle / avoidanceRadius);
|
||||
avoidForce = lateral.normalized * (maxAvoidForce * avoidanceStrength);
|
||||
}
|
||||
}
|
||||
|
||||
steering.Linear = arriveForce + avoidForce;
|
||||
|
||||
if (steering.Linear.magnitude > Agent.maxAcceleration)
|
||||
{
|
||||
steering.Linear = steering.Linear.normalized * Agent.maxAcceleration;
|
||||
}
|
||||
|
||||
return steering;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 102365b663634ccbaaf4b927b4d65075
|
||||
timeCreated: 1770448799
|
||||
@@ -20,6 +20,7 @@ namespace Code.UI
|
||||
|
||||
[SerializeField] private Button toMenuButton;
|
||||
[SerializeField] private Button resetButton;
|
||||
[SerializeField] private Button dopButton;
|
||||
|
||||
private Slider _arriveTargetRadiusSlider;
|
||||
private Slider _arriveSlowRadiusSlider;
|
||||
@@ -80,6 +81,8 @@ namespace Code.UI
|
||||
toMenuButton.onClick.AddListener(LoadMainMenu);
|
||||
if (resetButton != null)
|
||||
resetButton.onClick.AddListener(ResetAgents);
|
||||
if (dopButton != null)
|
||||
dopButton.onClick.AddListener(LoadDop);
|
||||
}
|
||||
|
||||
private void Start()
|
||||
@@ -247,6 +250,7 @@ namespace Code.UI
|
||||
}
|
||||
|
||||
private static void LoadMainMenu() => SceneManager.LoadScene("MainMenu");
|
||||
private static void LoadDop() => SceneManager.LoadScene("Lab2Dop");
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
@@ -266,6 +270,7 @@ namespace Code.UI
|
||||
|
||||
if (toMenuButton != null) toMenuButton.onClick.RemoveListener(LoadMainMenu);
|
||||
if (resetButton != null) resetButton.onClick.RemoveListener(ResetAgents);
|
||||
if (dopButton != null) resetButton.onClick.RemoveListener(LoadDop);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user