#How to move an object along a spline with rigidbody collision?

1 messages · Page 1 of 1 (latest)

pale flint
#

posting code in one minute

#

public void OnMove(InputValue value)
{
moveInput = value.Get<Vector2>(); // Stores input as a Vector2
}

void FixedUpdate()
{
if (splineContainer == null) return;

float movement = Input.GetAxis("Horizontal") + Input.GetAxis("Vertical");

//Calculate Spline Progress
float splineLength = splineContainer.CalculateLength();
progress += movement * (moveSpeed / splineLength) * Time.deltaTime;

//clamp progress to stay within spline bounds
progress = Mathf.Clamp01(progress);

//Apply the movement vector to the current position
float3 splinePos = splineContainer.EvaluatePosition(progress);
Vector2 position = new Vector2(splinePos.x, splinePos.y);
transform.position = (Vector3)splinePos;

rb.MovePosition((Vector3)splinePos + (Vector3)moveInput * Time.fixedDeltaTime * moveSpeed);
//rb.MovePosition(position + moveInput * Time.fixedDeltaTime * moveSpeed);

}

#

@unique cedar

#

ignore last //

#

my first thought is to see if next closest spline progress is within the collider and then not allowing for movement in that direction. but maybe theres an issue with my code as its still kinda a teleport using transform.position

#

but that was the only method i could find to convert transform.position into rb soz

unique cedar
#

Move position is better but yea ideally you would use force instead.

#

Btw the float to evaluate the spline is 0 to 1 and does not go up to the spine length