Bezier Path Creator - Optimised Paths
The Path creating and following method i was using was starting to get frustrating. Luckily i searching the asset store within unity and found another free option.
using UnityEngine;
namespace PathCreation.Examples
{
public class PathFollower : MonoBehaviour
{
public PathCreator pathCreator;
public EndOfPathInstruction endOfPathInstruction;
public float speed = 5;
float distanceTravelled;
void Start() {
if (pathCreator != null)
{
pathCreator.pathUpdated += OnPathChanged;
}
}
void Update()
{
if (pathCreator != null)
{
distanceTravelled += speed * Time.deltaTime;
transform.position = pathCreator.path.GetPointAtDistance(distanceTravelled, endOfPathInstruction);
transform.rotation = pathCreator.path.GetRotationAtDistance(distanceTravelled, endOfPathInstruction);
}
}
void OnPathChanged() {
distanceTravelled = pathCreator.path.GetClosestDistanceAlongPath(transform.position);
}
}
}
Creating the nodes for the path are self contained and the path can be create as curves. And when linking the car game object it worked smoothly. The the Z access doesnt change.
All the paths of the one to follow and the scenery cars.