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 Why this happens?

Discussion in 'Physics' started by LazyGhost15, Apr 2, 2023.

  1. LazyGhost15

    LazyGhost15

    Joined:
    Feb 12, 2022
    Posts:
    91
    I copied a moving system from YouTube to improve my chrecter movement. But the chrecter always moves a little and then stops and I dont know why. I think it might be connected to the X velocity in the rigidbody because the charecter always stops when the velocity drops to 0.
    This is the script:
    Code (CSharp):
    1.  Speed = _moveInput.x * MaxSpeed;
    2.  
    3.         if (Input.GetKey(KYBRight))
    4.         {
    5.             IsFacingRight = true;
    6.             IsMoving = true;
    7.             float SpeedDif = Speed - rigidbody2.velocity.x;
    8.             float movement = SpeedDif * accelaration;
    9.             rigidbody2.AddForce(movement * Vector2.right);
    10.             print(movement);
    11.            
    12.         }
    And here's a video of the problem:
    https://i.gyazo.com/9fc0703827eaa3647a92fe966f5afd2d.mp4
     
  2. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    538
    Is the ground made from multiple box colliders? What is the shape of collider of player character?

    Without knowing more my best guess is that corner of box collider is getting caught on the seam between multiple ground colliders.
    upload_2023-4-3_13-14-7.png

    You can mitigate it by making the corners of box colliders slightly round using edgeRadius property https://docs.unity3d.com/ScriptReference/BoxCollider2D-edgeRadius.html . It will change box with sharp corners, into a box with rounded corners. Just like in real life stuff with round corners is more likely to slide smoothly. Link to scripting API but it can also be set in the editor. As you increase the radius you might need to decrease the box collider size to maintain the overall dimensions.

    upload_2023-4-3_13-14-48.png