1
\$\begingroup\$

I am exploring the Unity Spline package, and within the package, there is a sample scene called 'splineAnimate' where the SplineAnimate script is used to move an object along a spline. The script has a 'speed' property in the inspector. However, when I set it to a different value, the object jumps along the spline instead of smoothly increasing or decreasing its speed. This issue occurs both when I increase and decrease the speed. I have captured the behavior here.

\$\endgroup\$
3
  • \$\begingroup\$ I am having the exact same problem but I noticed that increasing the 'MaxSpeed' in script will work fine but decreasing it resets the position as you say. This is the basic script I have written to test the new package. \$\endgroup\$
    – Ambo100
    Jun 28, 2023 at 16:05
  • \$\begingroup\$ So did you find the solution? \$\endgroup\$ Jul 3, 2023 at 7:01
  • \$\begingroup\$ Sadly not, my theory is that it has something to do with interpolation for speed. I am trying to figure out a way to set the position on the spline in code manually. \$\endgroup\$
    – Ambo100
    Jul 3, 2023 at 16:18

2 Answers 2

0
\$\begingroup\$

I have found a tutorial on YouTube that explains how to write a custom script to handle animating along a spline, it is a replacement for the SplineAnimate component. It handles positive and negative changes to the speed and automatically orients the GameObject to follow the spline.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Splines;

public class MoveAlongeSpline: MonoBehaviour
{
    public SplineContainer spline;
    public float speed = 1f;
    float distancePercentage = 0f;

    float splineLength;

    void Start()
    {
        splineLength = spline.CalculateLength();
    }

    void Update()
    {
        distancePercentage += speed * Time.deltaTime / splineLength;

        Vector3 currentPosition = spline.EvaluatePosition(distancePercentage);
        transform.position = currentPosition;

        if (distancePercentage > 1f)
        {
            distancePercentage = 0f;
        }

        Vector3 nextPosition = spline.EvaluatePosition(distancePercentage + 0.05f);
        Vector3 direction = nextPosition - currentPosition;
        transform.rotation = Quaternion.LookRotation(direction, transform.up);
    }
}
\$\endgroup\$
0
\$\begingroup\$

I think the function here teleports the object, for example it needs to go from point a to point b and it does it by teleporting... you should optimize the function by script.. instead of teleporting, you should make it drag from point a to point b

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Splines;

public class MoveAlongSpline : MonoBehaviour
{
    public SplineContainer spline;
    public float moveSpeed = 1f;
    public float rotationSpeed = 5f;

    private float currentDistance = 0f;

    void Update()
    {
        // Calculate the target position on the spline
        Vector3 targetPosition = spline.EvaluatePosition(currentDistance);

        // Move the character towards the target position on the spline
        transform.position = Vector3.MoveTowards(transform.position, targetPosition, moveSpeed * Time.deltaTime);

        // Calculate the target rotation on the spline
        Vector3 targetDirection = spline.EvaluateTangent(currentDistance);

        // Rotate the character towards the target rotation on the spline
        if (targetDirection != Vector3.zero)
        {
            Quaternion targetRotation = Quaternion.LookRotation(targetDirection, transform.up);
            transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
        }

        // If the end of the spline is reached, loop back to the beginning
        if (currentDistance >= 1f)
        {
            currentDistance = 0f;
        }
        else
        {
            // Adjust the movement based on the length of the spline
            float splineLength = spline.CalculateLength();
            float movement = moveSpeed * Time.deltaTime / splineLength;
            currentDistance += movement;
        }
    }
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .