1

I've got some code that moves the players in increments of 10 on the x and z axis using a slerp. However, the current slerp setup is slightly off. For example, if my start position is (10, 1, 0), and my end position is (20, 1, 0), during the slerp my player experiences slight variance on the y and z axis as well, however small it is. The player always starts and ends in the exact correct position, meaning it's an issue with the slerp itself. What am I missing that is causing curvature when slerp-ing?

...
public void Forward(InputAction.CallbackContext context){ 
        

        if (context.started && Rotating == false && Moving == false) { // checks if player can move
                percentageComplete = 0; //lerp percentage
                startPosition = rb.transform.position;
                endPosition = rb.transform.position + rb.transform.rotation * Vector3.forward * 10;

                endPosition[1] = 1;
                endPosition[0] = Mathf.Round(endPosition[0]);
                endPosition[2] = Mathf.Round(endPosition[2]);
                CheckPOS(); //Checks if endPosition is a valid position the player can move to, sets Moving to true
                }    
            }
...
    public void Update() { 

    if (Moving == true){
        if (percentageComplete < 1) {
            elapsedTime += Time.deltaTime;
            percentageComplete = elapsedTime / desiredDuration;
            rb.transform.position = Vector3.Slerp(startPosition, endPosition, percentageComplete);
        if (percentageComplete > 1) {
            posX = Mathf.Round(rb.transform.position.x / 10);
            posY = Mathf.Round(rb.transform.position.z / 10);
            rb.transform.position = new Vector3(posX * 10, 1, posY * 10);
            Moving = false;
                              }}

                       }

                   }
Chris W.
  • 21
  • 1

1 Answers1

0

Slerp is a spherical interpolation, which is normally used when you want to interpolate between different direction vectors. In this particular case, you're changing both direction and magnitude (direction only slightly, as (10, 1, 0) vs (20, 1, 0) is almost the same direction), so there's a very small rotation component, but mostly a change in magnitude.

If what you want is to linearly interpolate between two positions, consider using Lerp instead.

Dan Bryant
  • 27,329
  • 4
  • 56
  • 102