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

Bug Moving through colliders

Discussion in 'Physics' started by DZZc0rd, Jun 12, 2023.

  1. DZZc0rd

    DZZc0rd

    Joined:
    Sep 4, 2021
    Posts:
    58
    Hi, I made the player move and added the script, Rigidbody and Mesh Collider to the player object. But I have a problem, the player can pass through the colliders if the player collides with a ladder (Under it) and a wall and walks towards the wall. The record of this bug in the video below

    Here code:
    Code (CSharp):
    1. [SerializeField] float speed;
    2.  
    3. private void FixedUpdate()
    4. {
    5.        transform.position += transform.TransformDirection(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")) * speed * Time.deltaTime;
    6. }
    Video:


    Version: 2021.3.14f1
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    You've taken charge of the Transform position and are overriding what the Rigidbody is trying to do which is write to the Transform. If you want to use physics for movement then use the Rigidbody API and let it write to the Transform.

    That's standard physics shown in all the tutorials.
     
    DevDunk likes this.
  3. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,362
    That's because you set the position to inside of the wall, so it will go there.

    1. For teleporting/setting positions on rigidbodies use rigidbody.move

    2. To keep physics working change the velocity instead. Either directly or with addforce
     
  4. DZZc0rd

    DZZc0rd

    Joined:
    Sep 4, 2021
    Posts:
    58
    I want to use 1st solution, but can you make it?
     
  5. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,362
    I'm not going to make your project no. Google the documentation and see the sample code
     
  6. DZZc0rd

    DZZc0rd

    Joined:
    Sep 4, 2021
    Posts:
    58
    I got it, but what do you mean by rigidbody.move?
     
  7. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,362
    Google it and you shall find
    It's actually moveposition, but if you read the documentation you can see what it is.

    Using this might not fully fix the issue since you still set the position, so maybe use addforce or velocity
     
  8. DZZc0rd

    DZZc0rd

    Joined:
    Sep 4, 2021
    Posts:
    58
    Nah, doesn't help
     
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
  10. DZZc0rd

    DZZc0rd

    Joined:
    Sep 4, 2021
    Posts:
    58
  11. luskos

    luskos

    Joined:
    Mar 4, 2013
    Posts:
    48
    I believe it only appears that the problem is the wall, while the reason you can't go outside from the inside is because the Collision with the ladder outside is preventing it as if the collision is working inside but not outside. Check if it's all in order with the collision of the wall itself. Could be a check on IsTrigger of Collision component of the wall.