scripts 1 var

This commit is contained in:
Valya 2026-03-19 02:02:28 +03:00
parent 6df424ea8e
commit 1798ac3fc5
10 changed files with 364 additions and 0 deletions

58
Assets/Agent.cs Normal file
View File

@ -0,0 +1,58 @@
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.angular == 0.0f)
{
rotation = 0.0f;
}
if (steering.linear.sqrMagnitude == 0.0f)
{
velocity = Vector3.zero;
}
steering = new Steering();
}
}

23
Assets/AgentBehaviour.cs Normal file
View File

@ -0,0 +1,23 @@
using UnityEngine;
using System.Collections;
public class AgentBehaviour : MonoBehaviour
{
public GameObject target;
protected Agent agent;
public virtual void Awake()
{
agent = gameObject.GetComponent<Agent>();
}
public virtual void Update()
{
agent.SetSteering(GetSteering());
}
public virtual Steering GetSteering()
{
return new Steering();
}
}

44
Assets/Align.cs Normal file
View File

@ -0,0 +1,44 @@
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;
}
}

32
Assets/Face.cs Normal file
View File

@ -0,0 +1,32 @@
using UnityEngine;
using System.Collections;
public class Face : Align
{
protected GameObject targetAux;
public override void Awake()
{
base.Awake();
targetAux = target;
target = new GameObject();
target.AddComponent<Agent>();
}
void OnDestroy()
{
Destroy(target);
}
public override Steering GetSteering()
{
Vector3 direction = targetAux.transform.position - transform.position;
if (direction.magnitude > 0.0f)
{
float targetOrientation = Mathf.Atan2(direction.x, direction.z);
targetOrientation *= Mathf.Rad2Deg;
target.GetComponent<Agent>().orientation = targetOrientation;
}
return base.GetSteering();
}
}

99
Assets/Path.cs Normal file
View File

@ -0,0 +1,99 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Path : MonoBehaviour
{
public List<GameObject> nodes;
List<PathSegment> segments;
void Start()
{
segments = GetSegments();
}
public List<PathSegment> GetSegments()
{
List<PathSegment> segments = new List<PathSegment>();
int i;
for (i = 0; i < nodes.Count - 1; i++)
{
Vector3 src = nodes[i].transform.position;
Vector3 dst = nodes[i + 1].transform.position;
PathSegment segment = new PathSegment(src, dst);
segments.Add(segment);
}
return segments;
}
public float GetParam(Vector3 position, float lastParam)
{
float param = 0f;
PathSegment currentSegment = null;
float tempParam = 0f;
foreach (PathSegment ps in segments)
{
tempParam += Vector3.Distance(ps.a, ps.b);
if (lastParam <= tempParam)
{
currentSegment = ps;
break;
}
}
if (currentSegment == null)
return 0f;
Vector3 currPos = position - currentSegment.a;
Vector3 segmentDirection = currentSegment.b - currentSegment.a;
segmentDirection.Normalize();
Vector3 pointinSegment = Vector3.Project(currPos, segmentDirection);
param = tempParam - Vector3.Distance(currentSegment.a, currentSegment.b);
param += pointinSegment.magnitude;
return param;
}
public Vector3 GetPosition(float param)
{
Vector3 position = Vector3.zero;
PathSegment currentSegment = null;
float tempParam = 0f;
foreach (PathSegment ps in segments)
{
tempParam += Vector3.Distance(ps.a, ps.b);
if (param <= tempParam)
{
currentSegment = ps;
break;
}
}
if (currentSegment == null)
return Vector3.zero;
Vector3 segmentDirection = currentSegment.b - currentSegment.a;
segmentDirection.Normalize();
tempParam -= Vector3.Distance(currentSegment.a, currentSegment.b);
tempParam = param - tempParam;
position = currentSegment.a + segmentDirection * tempParam;
return position;
}
void OnDrawGizmos()
{
Vector3 direction;
Color tmp = Gizmos.color;
Gizmos.color = Color.magenta;
int i;
for (i = 0; i < nodes.Count - 1; i++)
{
Vector3 src = nodes[i].transform.position;
Vector3 dst = nodes[i + 1].transform.position;
direction = dst - src;
Gizmos.DrawRay(src, direction);
}
Gizmos.color = tmp;
}
}

24
Assets/PathFollower.cs Normal file
View File

@ -0,0 +1,24 @@
using UnityEngine;
using System.Collections;
public class PathFollower : Seek
{
public Path path;
public float pathOffset = 0.0f;
float currentParam;
public override void Awake()
{
base.Awake();
target = new GameObject();
currentParam = 0f;
}
public override Steering GetSteering()
{
currentParam = path.GetParam(transform.position, currentParam);
float targetParam = currentParam + pathOffset;
target.transform.position = path.GetPosition(targetParam);
return base.GetSteering();
}
}

16
Assets/PathSegment.cs Normal file
View File

@ -0,0 +1,16 @@
using UnityEngine;
using System.Collections;
public class PathSegment
{
public Vector3 a;
public Vector3 b;
public PathSegment() : this(Vector3.zero, Vector3.zero) { }
public PathSegment(Vector3 a, Vector3 b)
{
this.a = a;
this.b = b;
}
}

14
Assets/Seek.cs Normal file
View File

@ -0,0 +1,14 @@
using UnityEngine;
using System.Collections;
public class Seek : AgentBehaviour
{
public override Steering GetSteering()
{
Steering steering = new Steering();
steering.linear = target.transform.position - transform.position;
steering.linear.Normalize();
steering.linear = steering.linear * agent.maxAccel;
return steering;
}
}

14
Assets/Steering.cs Normal file
View File

@ -0,0 +1,14 @@
using UnityEngine;
using System.Collections;
public class Steering
{
public float angular;
public Vector3 linear;
public Steering()
{
angular = 0.0f;
linear = new Vector3();
}
}

40
Assets/Wander.cs Normal file
View File

@ -0,0 +1,40 @@
using UnityEngine;
using System.Collections;
public class Wander : Face
{
public float offset;
public float radius;
public float rate;
public override void Awake()
{
target = new GameObject();
target.transform.position = transform.position;
base.Awake();
}
public override Steering GetSteering()
{
Steering steering = new Steering();
float wanderOrientation = Random.Range(-1.0f, 1.0f) * rate;
float targetOrientation = wanderOrientation + agent.orientation;
Vector3 orientationVec = GetOriAsVec(agent.orientation);
Vector3 targetPosition = (offset * orientationVec) + transform.position;
targetPosition = targetPosition + (GetOriAsVec(targetOrientation) * radius);
targetAux.transform.position = targetPosition;
steering = base.GetSteering();
steering.linear = targetAux.transform.position - transform.position;
steering.linear.Normalize();
steering.linear *= agent.maxAccel;
return steering;
}
public Vector3 GetOriAsVec(float orientation)
{
Vector3 vector = Vector3.zero;
vector.x = Mathf.Sin(orientation * Mathf.Deg2Rad) * 1.0f;
vector.z = Mathf.Cos(orientation * Mathf.Deg2Rad) * 1.0f;
return vector.normalized;
}
}