Created
July 15, 2014 08:28
-
-
Save ricardojmendez/88488a8550ea62bfa119 to your computer and use it in GitHub Desktop.
Sample steering for path behavior. See https://github.com/ricardojmendez/UnitySteer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using UnityEngine; | |
using UnitySteer; | |
namespace UnitySteer.Base | |
{ | |
/// <summary> | |
/// Steers a vehicle to follow a path | |
/// </summary> | |
/// <remarks> | |
/// Trivial path following class which will simply steer for each point | |
/// in Vector3Pathway. It is meant as an example of how to start integrating | |
/// path following with your agents. | |
/// | |
/// Movement *will* look robotic due to its simplicity. | |
/// </remarks> | |
[AddComponentMenu("UnitySteer/Steer/... for Path - Trivial")] | |
[System.Obsolete("This is only a trivial sample behavior, using it is NOT recommended", true)] | |
public class SteerForPathTrivial : Steering | |
{ | |
#region Private fields | |
[SerializeField] | |
bool _startsOnFirstSegment = true; | |
int _currentNode = -1; | |
Vector3Pathway _path; | |
#endregion | |
#region Public properties | |
/// <summary> | |
/// Path to follow | |
/// </summary> | |
public Vector3Pathway Path | |
{ | |
get { | |
return this._path; | |
} | |
set { | |
_path = value; | |
_currentNode = FindStartNode(); | |
} | |
} | |
/// <summary> | |
/// Indicates if we start following the path on the first segment, or on the closest one | |
/// </summary> | |
public bool StartsOnFirstSegment | |
{ | |
get { | |
return this._startsOnFirstSegment; | |
} | |
set { | |
_startsOnFirstSegment = value; | |
} | |
} | |
#endregion | |
/// <summary> | |
/// Force to apply to the vehicle | |
/// </summary> | |
/// <returns> | |
/// A <see cref="Vector3"/> | |
/// </returns> | |
protected override Vector3 CalculateForce () | |
{ | |
Vector3 force = Vector3.zero; | |
if (_path != null) | |
{ | |
while (force == Vector3.zero && _currentNode < _path.SegmentCount) | |
{ | |
var node = _path.Path[_currentNode]; | |
force = Vehicle.GetSeekVector(node, false); | |
if (force == Vector3.zero) | |
{ | |
_currentNode++; | |
} | |
} | |
} | |
return force; | |
} | |
int FindStartNode() | |
{ | |
int closest = -1; | |
if (_startsOnFirstSegment) | |
{ | |
closest = 0; | |
} | |
else | |
{ | |
float minDistance = float.MaxValue; | |
var pos = Vehicle.Position; | |
for(int i = 0; i < _path.SegmentCount; i++) | |
{ | |
Vector3 point = _path.Path[i]; | |
var distance = (point - pos).sqrMagnitude; | |
if (distance < minDistance) | |
{ | |
minDistance = distance; | |
closest = i; | |
} | |
} | |
} | |
return closest; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment