[ad_1]
I feel the perform right here teleports the item, for instance it must go from level a to level b and it does it by teleporting… it is best to optimize the perform by script.. as a substitute of teleporting, it is best to make it drag from level a to level b
utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;
utilizing UnityEngine.Splines;
public class MoveAlongSpline : MonoBehaviour
{
public SplineContainer spline;
public float moveSpeed = 1f;
public float rotationSpeed = 5f;
personal float currentDistance = 0f;
void Replace()
{
// Calculate the goal place on the spline
Vector3 targetPosition = spline.EvaluatePosition(currentDistance);
// Transfer the character in the direction of the goal place on the spline
remodel.place = Vector3.MoveTowards(remodel.place, targetPosition, moveSpeed * Time.deltaTime);
// Calculate the goal rotation on the spline
Vector3 targetDirection = spline.EvaluateTangent(currentDistance);
// Rotate the character in the direction of the goal rotation on the spline
if (targetDirection != Vector3.zero)
{
Quaternion targetRotation = Quaternion.LookRotation(targetDirection, remodel.up);
remodel.rotation = Quaternion.Slerp(remodel.rotation, targetRotation, rotationSpeed * Time.deltaTime);
}
// If the top of the spline is reached, loop again to the start
if (currentDistance >= 1f)
{
currentDistance = 0f;
}
else
{
// Modify the motion primarily based on the size of the spline
float splineLength = spline.CalculateLength();
float motion = moveSpeed * Time.deltaTime / splineLength;
currentDistance += motion;
}
}
}
[ad_2]