Search Unity

Question Character twitches

Discussion in 'Getting Started' started by dahgfjlkjdjrrkjrk, Jul 24, 2021.

  1. dahgfjlkjdjrrkjrk

    dahgfjlkjdjrrkjrk

    Joined:
    Jul 21, 2021
    Posts:
    1
    when added to the project SwipeController. The character stops moving forward and jerks to the left or right



    []PlayerController]

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.     private CharacterController controller;
    8.     private Vector3 dir;
    9.     [SerializeField] private int speed;
    10.  
    11.     private int lineToMove = 1;
    12.     public float lineDistance = 4;
    13.     void Start()
    14.     {
    15.         controller = GetComponent<CharacterController>();
    16.     }
    17.  
    18.     private void Update()
    19.     {
    20.         if (SwipeController.swipeLeft)
    21.         {
    22.             if (lineToMove < 2)
    23.                 lineToMove++;
    24.         }
    25.  
    26.         if (SwipeController.swipeRight)
    27.         {
    28.             if (lineToMove > 0)
    29.                 lineToMove--;
    30.         }
    31.  
    32.         Vector3 targetPosition = transform.position.z * transform.forward + transform.position.y * transform.up;
    33.         if (lineToMove == 0)
    34.             targetPosition += Vector3.left * lineDistance;
    35.         else if (lineToMove == 2)
    36.             targetPosition += Vector3.right * lineDistance;
    37.  
    38.         transform.position = targetPosition;
    39.     }
    40.  
    41.     void FixedUpdate()
    42.     {
    43.         dir.z = speed;
    44.         controller.Move(dir * Time.fixedDeltaTime);
    45.     }
    46. }
    47.  
    [SwipeController]

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SwipeController : MonoBehaviour
    6. {
    7.     public static bool tap, swipeLeft, swipeRight, swipeUp, swipeDown;
    8.     private bool isDraging = false;
    9.     private Vector2 startTouch, swipeDelta;
    10.  
    11.     private void Update()
    12.     {
    13.         tap = swipeDown = swipeUp = swipeLeft = swipeRight = false;
    14.         if (Input.GetMouseButtonDown(0))
    15.         {
    16.             tap = true;
    17.             isDraging = true;
    18.             startTouch = Input.mousePosition;
    19.         }
    20.         else if (Input.GetMouseButtonUp(0))
    21.         {
    22.             isDraging = false;
    23.             Reset();
    24.         }
    25.         #endregion
    26.  
    27.         if (Input.touches.Length > 0)
    28.         {
    29.             if (Input.touches[0].phase == TouchPhase.Began)
    30.             {
    31.                 tap = true;
    32.                 isDraging = true;
    33.                 startTouch = Input.touches[0].position;
    34.             }
    35.             else if (Input.touches[0].phase == TouchPhase.Ended || Input.touches[0].phase == TouchPhase.Canceled)
    36.             {
    37.                 isDraging = false;
    38.                 Reset();
    39.             }
    40.         }
    41.         #endregion
    42.  
    43.         swipeDelta = Vector2.zero;
    44.         if (isDraging)
    45.         {
    46.             if (Input.touches.Length < 0)
    47.                 swipeDelta = Input.touches[0].position - startTouch;
    48.             else if (Input.GetMouseButton(0))
    49.                 swipeDelta = (Vector2)Input.mousePosition - startTouch;
    50.         }
    51.  
    52.         if (swipeDelta.magnitude > 100)
    53.         {
    54.             float x = swipeDelta.x;
    55.             float y = swipeDelta.y;
    56.             if (Mathf.Abs(x) > Mathf.Abs(y))
    57.             {
    58.              
    59.                 if (x < 0)
    60.                     swipeLeft = true;
    61.                 else
    62.                     swipeRight = true;
    63.             }
    64.             else
    65.             {
    66.              
    67.                 if (y < 0)
    68.                     swipeDown = true;
    69.                 else
    70.                     swipeUp = true;
    71.             }
    72.  
    73.             Reset();
    74.         }
    75.  
    76.     }
    77.  
    78.     private void Reset()
    79.     {
    80.         startTouch = swipeDelta = Vector2.zero;
    81.         isDraging = false;
    82.     }
    83. }