Search Unity

Rigidbody 2D goes through collider (not high speed)

Discussion in 'Physics' started by Vaspix, Jun 25, 2019.

  1. Vaspix

    Vaspix

    Joined:
    Jun 8, 2019
    Posts:
    54
    Hello,
    I have a problem that my 2D Player wants to go through a 2D Collider if I keep holding the "move" button. (I tryed to fix this with a physic material 2D on the player).
    When the Player touches a wall and I than hold a key to move, he should just stand there, but he goes through the wall and after a few milliseconds, he goes back where he was, and than does it again untill I release the key. (It looks like a lag)
    I don't have this problem with the same but 3D environment.
    Does anyone know what to do with it? I've watched many 2D game tutorials, and I didn't see someone has this problem.
    Thank you!
     
  2. arahmitz

    arahmitz

    Joined:
    Jun 25, 2019
    Posts:
    25
    Can you give me the movement script? I believe it's the problem between transform and addforce.
     
  3. Vaspix

    Vaspix

    Joined:
    Jun 8, 2019
    Posts:
    54
    I use simply transform.Translate to move and rb.AddForce for jumping
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.     float Speed;
    8.     public float hJump = 6f;
    9.     public Rigidbody2D rb;
    10.     public bool isGrounded = false;
    11.  
    12.     public Animator animator;
    13.  
    14.     void Update()
    15.     {
    16.         Vector3 movement = new Vector3(Speed, 0, 0); // Move
    17.         Jump();
    18.  
    19.         if (Input.GetKey(KeyCode.D)) // Left
    20.         {
    21.             Speed = 7f;
    22.             transform.position += movement * Time.deltaTime;
    23.         }
    24.         else if (Input.GetKeyUp(KeyCode.D))
    25.         {
    26.             Speed = 0;
    27.         }
    28.  
    29.         if (Input.GetKey(KeyCode.A)) // Right
    30.         {
    31.             Speed = -7f;
    32.             transform.position += movement * Time.deltaTime;
    33.         }
    34.         else if (Input.GetKeyUp(KeyCode.D))
    35.         {
    36.             Speed = 0;
    37.         }
    38.  
    39.         if (Input.GetKey(KeyCode.A) && Input.GetKey(KeyCode.D)) // Stop
    40.         {
    41.             Speed = 0;
    42.         }
    43.  
    44.  
    45.         if (Speed > 0) // Move Info
    46.         {
    47.             print("Moves right");
    48.             animator.SetInteger("Move", 2);
    49.         }
    50.         if (Speed < 0)
    51.         {
    52.             print("Moves left");
    53.             animator.SetInteger("Move", 1);
    54.         }
    55.         if (Speed == 0)
    56.         {
    57.             print("Not moves");
    58.             animator.SetInteger("Move", 0);
    59.         }
    60.  
    61.         if (Input.GetKeyDown(KeyCode.D)) // Rotate Player
    62.         {
    63.             transform.rotation = Quaternion.Euler(0, 0, 0);
    64.         }
    65.         if (Input.GetKeyDown(KeyCode.A))
    66.         {
    67.             transform.rotation = Quaternion.Euler(0, -180, 0);
    68.         }
    69.     }
    70.     void Jump()
    71.     {
    72.         if (Input.GetKeyDown("space") && isGrounded == true) // Jump
    73.         {
    74.             rb.AddForce(new Vector2(0, hJump), ForceMode2D.Impulse);
    75.         }
    76.     }
    77.     private void OnCollisionEnter2D(Collision2D collision) // isGrounded
    78.     {
    79.         if (collision.gameObject.tag == "Ground")
    80.         {
    81.             isGrounded = true;
    82.         }
    83.     }
    84.     private void OnCollisionExit2D(Collision2D collision)
    85.     {
    86.         if (collision.gameObject.tag == "Ground")
    87.         {
    88.             isGrounded = false; ;
    89.         }
    90.     }
    91. }
     
  4. arahmitz

    arahmitz

    Joined:
    Jun 25, 2019
    Posts:
    25
    I believe I had the same problem with my first 2d rogue-like prototype.
    First of all - change the transform into addforce, that should do the trick.
    Second - why do you use vector3 in 2D environment?
     
  5. Vaspix

    Vaspix

    Joined:
    Jun 8, 2019
    Posts:
    54
    Yeah, rb.AddForce doing clean, but I wouldn't use it for a 2D platformer game for moving the player, or is there a way to make an instant force without acceleration and slide after releasing a key?
    (I didn't know how to use Vector2, but now I googled that and replace it, thx)
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    Changing the Transform just teleports the Rigidbody to that position in the slowest possible way which is the cause of your tunnelling. Never, under any circumstance, modify a Transform when you have a Rigidbody on it as that is the job of the Rigidbody component and you're undermining what it's trying to do.

    Instead use forces, directly modify the velocity or use MovePosition / MoveRotation.
     
    arahmitz likes this.
  7. Vaspix

    Vaspix

    Joined:
    Jun 8, 2019
    Posts:
    54
    Thank you!
     
    MelvMay likes this.
  8. jordanflammia

    jordanflammia

    Joined:
    Dec 12, 2021
    Posts:
    3
    rotation
     
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    Please don't necro threads like this.