Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question [2D] Right-left swiping without lifting a finger no longer works when the cam follows the player

Discussion in 'Input System' started by Titi1040, Oct 31, 2020.

  1. Titi1040

    Titi1040

    Joined:
    Oct 1, 2020
    Posts:
    1
    Hi !
    I'm currently creating a mobile game with unity.
    i've try many methods to move the character but I kept the right-left swiping like a slider on the left side of the screen to move and a click on the right side to jump.
    Everything was ok, I could swipe to right or left to move without lifting my finger to switch direction. But when i add the camera follow I'm stuck in one direction and the only way to switch is to lift my finger and and press again swiping to the other direction. Sometime i can switch directly after press the screen but after a second it's impossible to switch direction without lifting the finger. I've try many way to change the movement on the movement script or in the way to let the cam follow the player ( with a script or with a parent) but nothing change. And when I remove the camera follow everything goes normal.

    Here is my playercontroller script :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.  
    8.     [SerializeField]
    9.     private float movementSpeed;
    10.     [SerializeField]
    11.     private float groundCheckRadius;
    12.     [SerializeField]
    13.     private float jumpForce;
    14.     [SerializeField]
    15.     private float slopeCheckDistance;
    16.     [SerializeField]
    17.     private float maxSlopeAngle;
    18.     [SerializeField]
    19.     private Transform groundCheck;
    20.     [SerializeField]
    21.     private LayerMask whatIsGround;
    22.     [SerializeField]
    23.     private PhysicsMaterial2D noFriction;
    24.     [SerializeField]
    25.     private PhysicsMaterial2D fullFriction;
    26.  
    27.     public float xInput;
    28.     private float slopeDownAngle;
    29.     private float slopeSideAngle;
    30.     private float lastSlopeAngle;
    31.  
    32.     private int facingDirection = 1;
    33.  
    34.     private bool isGrounded;
    35.     public bool isOnSlope;
    36.     private bool isJumping;
    37.     private bool canWalkOnSlope;
    38.     private bool canJump;
    39.  
    40.     private Vector2 newVelocity;
    41.     private Vector2 newForce;
    42.     private Vector2 capsuleColliderSize;
    43.  
    44.     private Vector2 slopeNormalPerp;
    45.  
    46.     private Rigidbody2D rb;
    47.     private CapsuleCollider2D cc;
    48.  
    49.  
    50.     public Vector3 Velocity;
    51.     public Vector2 xForce;
    52.     public LayerMask collisionLayers;
    53.     Vector2 PressedPosition;
    54.     GameObject[] Sliders;
    55.     Slider ActiveSlider;
    56.     public float Magnitude;
    57.     public float Direction;
    58.     public float vitesse;
    59.     Vector3 x, z;
    60.     public Vector2 PressedPosition1 { get => PressedPosition; set => PressedPosition = value; }
    61.  
    62.     private void Start()
    63.     {
    64.         rb = GetComponent<Rigidbody2D>();
    65.         cc = GetComponent<CapsuleCollider2D>();
    66.         Sliders = GameObject.FindGameObjectsWithTag("Slider");
    67.         capsuleColliderSize = cc.size;
    68.     }
    69.  
    70.      void Update()
    71.     {
    72.      //   CheckInput();
    73.  
    74.         if (Input.GetMouseButtonDown(0))
    75.         {
    76.             PressedPosition1 = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    77.  
    78.             //Check si c'est dans la zone TouchArea
    79.             RaycastHit2D hit = Physics2D.Raycast(PressedPosition1, Vector2.zero);
    80.             if (hit.collider != null)
    81.             {
    82.                 if (hit.collider.gameObject.transform.parent != null)
    83.                 {
    84.                     for (int i = 0; i < Sliders.Length; i++)
    85.                     {
    86.                         if (Sliders[i] == hit.collider.gameObject.transform.parent.gameObject)
    87.                         {
    88.                             ActiveSlider = hit.collider.gameObject.transform.parent.gameObject.GetComponent<Slider>();
    89.                             UnityEngine.Debug.Log("Slider Active");
    90.                             break;
    91.                         }
    92.                     }
    93.                 }
    94.             }
    95.         }
    96.         else if (ActiveSlider != null && Input.GetMouseButton(0))
    97.         {
    98.             Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    99.             Vector2 diff = mousePos - PressedPosition1;
    100.  
    101.             //à quel distance on a bougé depuis le clic ?...
    102.             Magnitude = diff.magnitude / ActiveSlider.Sensitivity;
    103.             diff.Normalize();
    104.  
    105.             //...et dans quelle direction ?
    106.             Direction = diff.x < 0 ? -1 : 1;
    107.  
    108.             //bouger le slider
    109.             ActiveSlider.MoveSlider(Magnitude, Direction);
    110.         }
    111.         else if (ActiveSlider != null && Input.GetMouseButtonUp(0))
    112.         {
    113.             //clic fini, reset
    114.             PressedPosition1 = Vector2.zero;
    115.             Magnitude = 0;
    116.             Direction = 0;
    117.             ActiveSlider.Value = 0;
    118.             ActiveSlider.MoveSlider(0, 0);
    119.             ActiveSlider = null;
    120.         }
    121.  
    122.         /*
    123.          *
    124.          * MOUVEMENT CODE
    125.          *
    126.          *
    127.          */
    128.  
    129.         // Si il y a un slider actif, faire bouger le personnage
    130.         if (ActiveSlider != null)
    131.         {
    132.             Velocity += new Vector3(ActiveSlider.Value, 0, 0);
    133.             ApplyMovement();
    134.  
    135.         }
    136.         else
    137.         {
    138.             Velocity = Vector2.zero;
    139.         }
    140.     }
    141.  
    142.     private void FixedUpdate()
    143.     {
    144.         CheckGround();
    145.         SlopeCheck();
    146.         ApplyMovement();
    147.     }
    148.  
    149.     private void CheckInput()
    150.     {
    151.         xInput = Input.GetAxisRaw("Horizontal");
    152.  
    153.         if (xInput == 1 && facingDirection == -1)
    154.         {
    155.             Flip();
    156.         }
    157.         else if (xInput == -1 && facingDirection == 1)
    158.         {
    159.             Flip();
    160.         }
    161.  
    162.         if (Input.GetButtonDown("Jump"))
    163.         {
    164.             Jump();
    165.         }
    166.  
    167.     }
    168.     private void CheckGround()
    169.     {
    170.         isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
    171.  
    172.         if(rb.velocity.y <= 0.0f)
    173.         {
    174.             isJumping = false;
    175.         }
    176.  
    177.         if(isGrounded && !isJumping && slopeDownAngle <= maxSlopeAngle)
    178.         {
    179.             canJump = true;
    180.         }
    181.  
    182.     }
    183.  
    184.     private void SlopeCheck()
    185.     {
    186.         Vector2 checkPos = transform.position - (Vector3)(new Vector2(0.0f, capsuleColliderSize.y / 2));
    187.  
    188.         SlopeCheckHorizontal(checkPos);
    189.         SlopeCheckVertical(checkPos);
    190.     }
    191.  
    192.     private void SlopeCheckHorizontal(Vector2 checkPos)
    193.     {
    194.         RaycastHit2D slopeHitFront = Physics2D.Raycast(checkPos, transform.right, slopeCheckDistance, whatIsGround);
    195.         RaycastHit2D slopeHitBack = Physics2D.Raycast(checkPos, -transform.right, slopeCheckDistance, whatIsGround);
    196.  
    197.         if (slopeHitFront)
    198.         {
    199.             isOnSlope = true;
    200.  
    201.             slopeSideAngle = Vector2.Angle(slopeHitFront.normal, Vector2.up);
    202.  
    203.         }
    204.         else if (slopeHitBack)
    205.         {
    206.             isOnSlope = true;
    207.  
    208.             slopeSideAngle = Vector2.Angle(slopeHitBack.normal, Vector2.up);
    209.         }
    210.         else
    211.         {
    212.             slopeSideAngle = 0.0f;
    213.             isOnSlope = false;
    214.         }
    215.  
    216.     }
    217.  
    218.     private void SlopeCheckVertical(Vector2 checkPos)
    219.     {    
    220.         RaycastHit2D hit = Physics2D.Raycast(checkPos, Vector2.down, slopeCheckDistance, whatIsGround);
    221.  
    222.         if (hit)
    223.         {
    224.  
    225.             slopeNormalPerp = Vector2.Perpendicular(hit.normal).normalized;          
    226.  
    227.             slopeDownAngle = Vector2.Angle(hit.normal, Vector2.up);
    228.  
    229.             if(slopeDownAngle != lastSlopeAngle)
    230.             {
    231.                 isOnSlope = true;
    232.             }                      
    233.  
    234.             lastSlopeAngle = slopeDownAngle;
    235.          
    236.             Debug.DrawRay(hit.point, slopeNormalPerp, Color.blue);
    237.             Debug.DrawRay(hit.point, hit.normal, Color.green);
    238.  
    239.         }
    240.  
    241.         if (slopeDownAngle > maxSlopeAngle || slopeSideAngle > maxSlopeAngle)
    242.         {
    243.             canWalkOnSlope = false;
    244.         }
    245.         else
    246.         {
    247.             canWalkOnSlope = true;
    248.         }
    249.  
    250.         if (isOnSlope && canWalkOnSlope && Direction == 0.0f)
    251.         {
    252.             rb.sharedMaterial = fullFriction;
    253.         }
    254.         else
    255.         {
    256.             rb.sharedMaterial = noFriction;
    257.         }
    258.     }
    259.  
    260.     public void Jump()
    261.     {
    262.         if (canJump)
    263.         {
    264.             canJump = false;
    265.             isJumping = true;
    266.             newVelocity.Set(0.0f, 0.0f);
    267.             rb.velocity = newVelocity;
    268.             newForce.Set(0.0f, jumpForce);
    269.             rb.AddForce(newForce, ForceMode2D.Impulse);
    270.         }
    271.     }  
    272.  
    273.     private void ApplyMovement()
    274.     {
    275.         if (isGrounded && !isOnSlope && !isJumping) //if not on slope
    276.         {
    277.             Debug.Log("not slope");
    278.             newVelocity.Set(movementSpeed * Direction, 0.0f);
    279.             rb.velocity = newVelocity;
    280.          //   Velocity = Vector3.ClampMagnitude(Velocity, vitesse);
    281.          //   this.transform.position += Velocity * Time.deltaTime;
    282.         }
    283.         else if (isGrounded && isOnSlope && canWalkOnSlope && !isJumping) //If on slope
    284.         {
    285.             Debug.Log("slope");
    286.             newVelocity.Set(movementSpeed * slopeNormalPerp.x * -Direction, movementSpeed * slopeNormalPerp.y * -Direction);
    287.            // this.transform.position += newVelocity * *Time.deltaTime;
    288.            rb.velocity = newVelocity;
    289.         }
    290.         else if (!isGrounded) //If in air
    291.         {
    292.             Debug.Log("in the air");
    293.             newVelocity.Set(movementSpeed * Direction, rb.velocity.y);
    294.             rb.velocity = newVelocity;
    295.         }
    296.  
    297.     }
    298.  
    299.     private void Flip()
    300.     {
    301.         facingDirection *= -1;
    302.         transform.Rotate(0.0f, 180.0f, 0.0f);
    303.     }
    304.  
    305.     private void OnDrawGizmos()
    306.     {
    307.         Gizmos.DrawWireSphere(groundCheck.position, groundCheckRadius);
    308.     }
    309.  
    310. }
    311.  
    312.  
    And the slider script :
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3.  
    4.  
    5. public class Slider : MonoBehaviour {
    6.          
    7.    
    8.     public float Sensitivity = 1;
    9.     public float MaxDistance = 0.4f;
    10.     public float MoveSpeed = 10000;
    11.     public float Value = 0;
    12.     public float magnitude;
    13.     public float direction;
    14.     public float moveDistance;
    15.  
    16.     void Start () {
    17.          }
    18.  
    19.     public void MoveSlider(float magnitude, float direction)
    20.     {
    21.         float moveDistance = direction * MoveSpeed * magnitude * Time.deltaTime;
    22.         Value += moveDistance;
    23.      
    24.         Value = Mathf.Clamp(Value, -MaxDistance, MaxDistance);
    25.       //  UnityEngine.Debug.Log("moveDistance  " + moveDistance + "Value" + Value + "magnitude" + magnitude + "MaxDistance" + MaxDistance + "Time.deltaTime" + Time.deltaTime);
    26.  
    27.     }
    28. }
    29.  
    can you help me please ?