- Home /
How do I change this script to move object along spline ?
Hi, I just can't wrap my head around this script. I want to move my gameobject based on input along the spline. At now it moves as time goes by. I think i need to change the "progress += Time.deltaTime / duration;" but i dont really know how... I am using the BezierSpline-Script by: http://catlikecoding.com/unity/tutorials/curves-and-splines/ Here is the walker script: public class SplineWalker : MonoBehaviour {
public BezierSpline spline;
public float duration;
public bool lookForward;
public SplineWalkerMode mode;
private float progress;
private bool goingForward = true;
private void Update () {
if (goingForward) {
progress += Time.deltaTime / duration;
if (progress > 1f) {
if (mode == SplineWalkerMode.Once) {
progress = 1f;
}
else if (mode == SplineWalkerMode.Loop) {
progress -= 1f;
}
else {
progress = 2f - progress;
goingForward = false;
}
}
}
else {
progress -= Time.deltaTime / duration;
if (progress < 0f) {
progress = -progress;
goingForward = true;
}
}
Vector3 position = spline.GetPoint(progress);
transform.localPosition = position;
if (lookForward) {
transform.LookAt(position + spline.GetDirection(progress));
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612143700im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Apply Input.GetAxis Smoothing to Joystick Axis 0 Answers
Why is Input.GetAxisRaw() not returning whole numbers when using a joystick? 1 Answer
How do I use Joystick just to face/move forward? 1 Answer
,Look rotation viewing vector is zero and character always facing 1 direction when idle 0 Answers