Unity3d Vector3.Lerp doesn't work correctly

Viewed 49

I want the character to slide right or left while walking on the straight road. The character will move to the right or left when the user scrolls the screen right or left. However, when the character moves to the right, it stays at a lower value when it should move to 12.5 on the x-axis. Also, when I increase the coefficient of time.deltatime, the speed increases, but the object looks like it is teleporting.

public void Swipe()
    {
        Vector3 left = new Vector3(-12.5f, transform.position.y, transform.position.z);
        Vector3 right = new Vector3(12.5f, transform.position.y, transform.position.z);
        playersPosition = transform.position;
        // If player touch screen FIRST-ONE time  
        if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
        {
            startTouchPosition = Input.GetTouch(0).position;
        }

        if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
        {
            currentPosition = Input.GetTouch(0).position;
            Vector3 Distance = currentPosition - startTouchPosition;
            if (!stopTouch)
            {
                if (Distance.x < -swipeRange)
                {
                    //Statement
                    Debug.Log("left");
                    transform.position = Vector3.Lerp(playersPosition,left, 5 * Time.deltaTime );
                    stopTouch = true;
                }

                else if (Distance.x > swipeRange)
                {
                    //Statement
                    transform.position = Vector3.Lerp(playersPosition,right, 5 * Time.deltaTime );
                    Debug.Log("Right");
                    stopTouch = true;
                }
                
            }
        }

        if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended)
        {
            stopTouch = false;
            endTouchPosition = Input.GetTouch(0).position;
            Vector3 Distance = endTouchPosition - startTouchPosition;
            
        }
        
    }
0 Answers
Related