1
\$\begingroup\$

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?

\$\endgroup\$
4
  • \$\begingroup\$ Well, you know how long the path is. You know how long you want it to take. Based on that you should be able to calculate the speed. And you would do that each time you reach a new waypoint. \$\endgroup\$
    – Zibelas
    Nov 2 at 13:44
  • \$\begingroup\$ That would work if I want a constant speed along all the phe path, but if I want to know the speed between each point, I would need the distance to the next point, is it possibe to get it? i did not find it in the doumentation for the asset. \$\endgroup\$
    – Alejandro
    Nov 3 at 6:40
  • \$\begingroup\$ If the asset has no option to get segments, an easy workaround would be to generate a second path that just contains one segment. \$\endgroup\$
    – Zibelas
    Nov 3 at 7:36
  • \$\begingroup\$ That is a good idea. I also found that I can obtain the closest distance of the path to a given point so i think I can make it work with that. Thanks for the help, I´'ll post the solution if it works. \$\endgroup\$
    – Alejandro
    Nov 6 at 7:27

0

You must log in to answer this question.

Browse other questions tagged .