I am using the Bézier Path Creator to create paths for an object. The idea is that I have a matrix of arrays like this this:
[time,X,Y,Z]
The objective is to create a path using the XYZ points and each point must be reached at the time associated to it, so the speed varies along the path. For now I managed to create a path with those XYZ points with this script:
using System.Collections.Generic;
using UnityEngine;
namespace PathCreation.Examples {
[RequireComponent(typeof(PathCreator))]
public class CreatePath : MonoBehaviour {
// Variables for the points along the path and if it is a closed loop or not
public bool closedLoop = true;
public List<Vector3> waypoints = new List<Vector3>();
// Creates the path from the given points
public void GeneratePath()
{
if (waypoints.Count > 0)
{
// Create a new bezier path from the waypoints.
BezierPath bezierPath = new BezierPath(waypoints, closedLoop, PathSpace.xyz);
GetComponent<PathCreator>().bezierPath = bezierPath;
}
}
void Start () {
if (waypoints.Count> 0) {
BezierPath bezierPath = new BezierPath(waypoints, closedLoop, PathSpace.xyz);
GetComponent<PathCreator>().bezierPath = bezierPath;
}
}
}
}
For the follower I found this, which allows me to reduce the speed along all the path:
using UnityEngine;
using PathCreation;
public class Follower : MonoBehaviour
{
public float speed = 5;
public EndOfPathInstruction loopMode = EndOfPathInstruction.Loop;
public AnimationCurve speedAlongPath = AnimationCurve.EaseInOut(0f, 1f, 1f, 0.1f);
[Space]
public PathCreator pathCreator;
private float distance;
private void Update()
{
var path = pathCreator.path;
// Get the normalized distance along the path (in a 0 to 1 range)
var t = (distance % path.length) / path.length;
// Get the new speed from the curve
var curveSpeed = speedAlongPath.Evaluate(t);
// Move based on the global speed multiplied by the speed sampled from the curve
distance += speed * curveSpeed * Time.deltaTime;
transform.SetPositionAndRotation(path.GetPointAtDistance(distance, loopMode), path.GetRotationAtDistance(distance, loopMode));
}
}
I want to make it so that speed varies in a way that each point is reached at the value of time for that point. Any ideas on how to do it?