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.
  2. Dismiss Notice

Question Unity2D Rigidbody2D.Velocity clipping through walls.

Discussion in 'Physics' started by DK_Kalach, Sep 6, 2021.

  1. DK_Kalach

    DK_Kalach

    Joined:
    Sep 2, 2021
    Posts:
    13
    Input Script:

    Code (CSharp):
    1.     private void FixedUpdate()
    2.     {
    3.         //Speed
    4.         InputX = Input.GetAxisRaw("Horizontal");
    5.         InputY = Input.GetAxisRaw("Vertical");
    6.  
    7.         Movement = new Vector2(InputX, InputY);
    8.  
    9.         if (InputX != 0 || InputY != 0)
    10.         {
    11.             Player_Manager_S.Player_Movement_S.Walking();
    12.         }
    13.  
    14.  
    Movement Script:

    Code (CSharp):
    1.     public void Walking()
    2.     {
    3.         rb.velocity = Movement * Speed * Time.deltaTime;
    4.     }
    And result in the video:


    (If you have any ideas why it's happening I would be more than happy to know
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,513
    First, this line makes no sense. You do not need to set an absolute velocity by scaling it by the current elapsed time. Velocity will be time-integrated to position later.

    You don't show how it's rotating which is likely you're changing the Transform or just instantly changing it causing overlaps. Because the physics system cannot use velocity adequately to keep them separated, because you're stomping over it, it gets into an impossible situation.

    Changing it to use Continuous Collision Detection might help but it won't stop the overlaps you're causing.
     
  3. DK_Kalach

    DK_Kalach

    Joined:
    Sep 2, 2021
    Posts:
    13
    Ohhhhhh right!!!! The rotation, I completely forgot about that, I've checked and you were right, I fixed the code and it's almost perfect now, Thanks for answering & the tips as well, Really appreciate that.
    Have a good day :)
     
    MelvMay likes this.
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,513
    Good luck.