#How to move an object along a spline with rigidbody collision?
1 messages · Page 1 of 1 (latest)
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