how to remove jerk in player movement?

52 Views Asked by At
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class swipeMovement : MonoBehaviour
{
    private float movementSpeed = 0.0055f;
    private Touch touch;
    Vector3 clampPos;
    public bool canswipe;
    private Vector3 initialTouchPosition;
    bool isSwiping;
    // Start is called before the first frame update
    void Start()
    {
        isSwiping = false;
        canswipe = false;
    }
 
    // Update is called once per frame
    void FixedUpdate()
    {
        swipeMove();
    }
 
    /* void Update()
     {
         if (canswipe)
         {
             HandleSwipeInput();
         }
     }
    /
     private void HandleSwipeInput()
     {
         if (Input.touchCount > 0)
         {
             Touch touch = Input.GetTouch(0);
 
             switch (touch.phase)
             {
                 case TouchPhase.Began:
                     initialTouchPosition = transform.position;
                     break;
 
                 case TouchPhase.Moved:
                     clampPos = new Vector3(
                         Mathf.Clamp(initialTouchPosition.x + touch.deltaPosition.x * movementSpeed, -2.2f, 2f),
                         transform.position.y,
                         transform.position.z
                     );
                     transform.position = clampPos;
                     break;
             }
         }
        */
       
    private void swipeMove()
    {
        if (canswipe)
        {
            if (isSwiping == false)
            {
                if (Input.touchCount > 0)
                {
                    touch = Input.GetTouch(0);

                    if (touch.phase == TouchPhase.Moved)
                    {
                        isSwiping = true;
                        clampPos = new Vector3(Mathf.Clamp(transform.position.x, -2.2f, 2f) + touch.deltaPosition.x * movementSpeed, transform.position.y, transform.position.z);
                        // transform.position = new Vector3(transform.position.x + touch.deltaPosition.x * movementSpeed, transform.position.y, transform.position.z);
                        transform.position = clampPos;
                    }

                    if (transform.position == clampPos)
                    {
                        isSwiping = false;
                    }
                }
            }
        }
    }
}

//player

playerrb.velocity = transform.forward * PlayerSpeed;

I made a player object which moves forward with a constant speed in z axis...and I am adding swipe movement to player through which player can swipe left and right in x axis … but when I swipe at low speed the jerk is little but when i increases the speed the jerk is getting bigger by holding down the touch and swiping with strong touch.. I am not getting it why.. can you guys plz suggest me why the jerk is occurring and what is the solution to it... code for player moving forward is:

0

There are 0 best solutions below