Path-Creator
Path-Creator copied to clipboard
How check end of path
I'm developing a race car sims, i need to check end of path (race start line), it's possibile with path creator object or i need a collider object to solve the problem?
I'm developing a race car sims, i need to check end of path (race start line), it's possibile with path creator object or i need a collider object to solve the problem?
Scanning through the source code, there is no events that you can handle when the path following has ended, reversed or looped.
However, if you just want to know if the follower has reached the end of the path, then you can check if the follower's transform position is equal to the path's last point.
Here is an example that will scale the follower by 10 (x, y, z) when it reaches the end of the path:
void Update()
{
if (pathCreator != null)
{
distanceTravelled += speed * Time.deltaTime;
transform.position = pathCreator.path.GetPointAtDistance(distanceTravelled, endOfPathInstruction);
transform.rotation = pathCreator.path.GetRotationAtDistance(distanceTravelled, endOfPathInstruction);
if (transform.position == pathCreator.path.GetPoint(pathCreator.path.NumPoints - 1))
{
// Put whatever code you want to run here when the follower reaches the end of the path
transform.localScale = new Vector3(10, 10, 10);
}
}
}
To further this, you could create your own path follower script like the examples that only always uses the stop end of path instruction and fire a unity event to say it has reached the end of the path. Then you can add "actions" that should happen at the end of the path via the inspector as you may have done with other scripts/components etc.
using UnityEngine;
using UnityEngine.Events;
namespace PathCreation.Examples
{
// Moves along a path at constant speed, will stop at the end and fire unity event.
public class ToEndPathFollower : MonoBehaviour
{
public PathCreator pathCreator;
public float speed = 5;
public UnityEvent reachedEndOfPath;
float distanceTravelled;
void Start() {
if (pathCreator != null)
{
// Subscribed to the pathUpdated event so that we're notified if the path changes during the game
pathCreator.pathUpdated += OnPathChanged;
}
}
void Update()
{
if (pathCreator != null)
{
distanceTravelled += speed * Time.deltaTime;
transform.position = pathCreator.path.GetPointAtDistance(distanceTravelled, EndOfPathInstruction.Stop);
transform.rotation = pathCreator.path.GetRotationAtDistance(distanceTravelled, EndOfPathInstruction.Stop);
if (transform.position == pathCreator.path.GetPoint(pathCreator.path.NumPoints - 1))
{
reachedEndOfPath.Invoke();
}
}
}
// If the path changes during the game, update the distance travelled so that the follower's position on the new path
// is as close as possible to its position on the old path
void OnPathChanged() {
distanceTravelled = pathCreator.path.GetClosestDistanceAlongPath(transform.position);
}
}
}
thanks, ShanYu for your solution to get the end position of the pathCreator. I was trying to do a reverse travel from endpoint to start point for a liquid simulation using this PathCreation package from the unity store. But I couldn't get the endpoint position using getPoint(1) from the documentation. Luckily your pathCreator.path.NumPoints - 1 helps to solve my problem.
using UnityEngine; using PathCreation;
public class NegativeFlow : MonoBehaviour { public PathCreator pathCreator; public float speed = 5f; public float trailTime = 5f;
public float distanceTravelled;
TrailRenderer trail;
[HideInInspector]
public bool isFlowing = false;
[HideInInspector]
public bool pathIsEnd = false;
public EndOfPathInstruction endPath;
private void Start()
{
trail = GetComponent<TrailRenderer>();
endPath = EndOfPathInstruction.Loop;
}
private void Update()
{
if (isFlowing)
{
transform.position = pathCreator.path.GetPoint(pathCreator.path.NumPoints - 1);
distanceTravelled -= speed * Time.deltaTime;
trail.time = trailTime;
transform.position = pathCreator.path.GetPointAtDistance(distanceTravelled, endPath);
transform.rotation = pathCreator.path.GetRotationAtDistance(distanceTravelled, endPath);
if (distanceTravelled <= pathCreator.path.length)
{
pathIsEnd = true;
}
if (CompareDistance(transform.position, pathCreator.path.GetPoint(0)))
{
endPath = EndOfPathInstruction.Stop;
}
}
else
{
trail.time = 0;
distanceTravelled = 0;
transform.position = pathCreator.path.GetPoint(pathCreator.path.NumPoints - 1);
endPath = EndOfPathInstruction.Loop;
pathIsEnd = false;
}
}
public bool CompareDistance(Vector3 liquid, Vector3 endPoint)
{
return Vector3.SqrMagnitude(liquid - endPoint) < 0.0001f;
}
}
``
public float distanceTravelled; TrailRenderer trail;
[HideInInspector] public bool isFlowing = false; [HideInInspector] public bool pathIsEnd = false;
public EndOfPathInstruction endPath;
private void Start() { trail = GetComponent<TrailRenderer>(); endPath = EndOfPathInstruction.Loop;
I see you refrenced to a Trail Renderer. I added it as a component, but now nothing moves anymore. Do I really need this component to reverse the path after it ends?