From 1798ac3fc530a1289ce4a23b31961b3f6de026f6 Mon Sep 17 00:00:00 2001 From: Valya Date: Thu, 19 Mar 2026 02:02:28 +0300 Subject: [PATCH] scripts 1 var --- Assets/Agent.cs | 58 +++++++++++++++++++++++ Assets/AgentBehaviour.cs | 23 ++++++++++ Assets/Align.cs | 44 ++++++++++++++++++ Assets/Face.cs | 32 +++++++++++++ Assets/Path.cs | 99 ++++++++++++++++++++++++++++++++++++++++ Assets/PathFollower.cs | 24 ++++++++++ Assets/PathSegment.cs | 16 +++++++ Assets/Seek.cs | 14 ++++++ Assets/Steering.cs | 14 ++++++ Assets/Wander.cs | 40 ++++++++++++++++ 10 files changed, 364 insertions(+) create mode 100644 Assets/Agent.cs create mode 100644 Assets/AgentBehaviour.cs create mode 100644 Assets/Align.cs create mode 100644 Assets/Face.cs create mode 100644 Assets/Path.cs create mode 100644 Assets/PathFollower.cs create mode 100644 Assets/PathSegment.cs create mode 100644 Assets/Seek.cs create mode 100644 Assets/Steering.cs create mode 100644 Assets/Wander.cs diff --git a/Assets/Agent.cs b/Assets/Agent.cs new file mode 100644 index 0000000..2155912 --- /dev/null +++ b/Assets/Agent.cs @@ -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(); + } +} diff --git a/Assets/AgentBehaviour.cs b/Assets/AgentBehaviour.cs new file mode 100644 index 0000000..5a11364 --- /dev/null +++ b/Assets/AgentBehaviour.cs @@ -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(); + } + + public virtual void Update() + { + agent.SetSteering(GetSteering()); + } + + public virtual Steering GetSteering() + { + return new Steering(); + } +} diff --git a/Assets/Align.cs b/Assets/Align.cs new file mode 100644 index 0000000..67efdb3 --- /dev/null +++ b/Assets/Align.cs @@ -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().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; + } +} diff --git a/Assets/Face.cs b/Assets/Face.cs new file mode 100644 index 0000000..8abb82a --- /dev/null +++ b/Assets/Face.cs @@ -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(); + } + + 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().orientation = targetOrientation; + } + return base.GetSteering(); + } +} diff --git a/Assets/Path.cs b/Assets/Path.cs new file mode 100644 index 0000000..bc6b83a --- /dev/null +++ b/Assets/Path.cs @@ -0,0 +1,99 @@ +using UnityEngine; +using System.Collections; +using System.Collections.Generic; + +public class Path : MonoBehaviour +{ + public List nodes; + List segments; + + void Start() + { + segments = GetSegments(); + } + + public List GetSegments() + { + List segments = new List(); + 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; + } +} diff --git a/Assets/PathFollower.cs b/Assets/PathFollower.cs new file mode 100644 index 0000000..b112903 --- /dev/null +++ b/Assets/PathFollower.cs @@ -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(); + } +} diff --git a/Assets/PathSegment.cs b/Assets/PathSegment.cs new file mode 100644 index 0000000..9f1397a --- /dev/null +++ b/Assets/PathSegment.cs @@ -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; + } +} diff --git a/Assets/Seek.cs b/Assets/Seek.cs new file mode 100644 index 0000000..44d9150 --- /dev/null +++ b/Assets/Seek.cs @@ -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; + } +} diff --git a/Assets/Steering.cs b/Assets/Steering.cs new file mode 100644 index 0000000..8b6e828 --- /dev/null +++ b/Assets/Steering.cs @@ -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(); + } +} diff --git a/Assets/Wander.cs b/Assets/Wander.cs new file mode 100644 index 0000000..9e378c5 --- /dev/null +++ b/Assets/Wander.cs @@ -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; + } +}