Search Unity

Player keeps glitching through walls

Discussion in 'World Building' started by lucadandois, Sep 11, 2018.

  1. lucadandois

    lucadandois

    Joined:
    Sep 11, 2018
    Posts:
    12
    I'm making a game in wich the goal is to escape a box by moving the player against other blocks to line the player up with the exit but the problem is that the player keeps glitching true the walls of the box. see the pictures for more information about the setup. If someone can help me with this it woud be great.
    I'm using a C# script for the player movement. the only borders are the walls added to the side as you can see in the pictures.

    upload_2018-9-11_18-52-58.png

    upload_2018-9-11_18-54-10.png

    upload_2018-9-11_18-54-38.png

    upload_2018-9-11_18-54-59.png

    upload_2018-9-11_18-56-21.png
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    If you move the player by setting the Transform.position or the Rigidbody.position value, it will teleport into the wall without physics.

    What you want is to use the Rigidbody.MovePosition() function, which will cause physics to keep the player out, assuming you are moving it by a reasonable amount and that your collision checking method is appropriate to the task.
     
  3. lucadandois

    lucadandois

    Joined:
    Sep 11, 2018
    Posts:
    12
    this is the current script i'm using for the player movement i forgot to add it to the post

    upload_2018-9-26_13-9-27.png
     
  4. lucadandois

    lucadandois

    Joined:
    Sep 11, 2018
    Posts:
    12
    If I change the script to this
    upload_2018-9-26_13-12-19.png

    Unity gives an error:
    upload_2018-9-26_13-12-53.png
     
  5. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    966
    I had an issue where AI was able to push my player through walls. I solved the problem by recording the last known position. On the next frame, I see where it is and do a raycast to make sure that it didn’t intersect a wall. If it did I warp the player to the previous position.
     
  6. Serinx

    Serinx

    Joined:
    Mar 31, 2014
    Posts:
    788
    AddForce should work fine. I've had this problem when the object is moving too fast, how fast does the player move?
    Using a raycast as newjerseyrunner suggested is a way to get around it. e.g. you could raycast in the direction you're intending to move (with a short ray), if the ray hits a wall then don't apply the force.

    Also, what's in the else part of those if statements? That could be important.

    On an unrelated note, the word you are looking for is "through" not "true". i.e. "glitching through walls"
     
  7. lucadandois

    lucadandois

    Joined:
    Sep 11, 2018
    Posts:
    12
     
  8. lucadandois

    lucadandois

    Joined:
    Sep 11, 2018
    Posts:
    12
    Can you maybe give an example script? I can't figure out how to change my script to make it work.
     
  9. Serinx

    Serinx

    Joined:
    Mar 31, 2014
    Posts:
    788
    You could try something like this, I haven't tested it, but essentially what it's saying is that we only move left if there is not wall to the left of the player, I used rb.velocity.magnitude for the distance parameter so that you can move towards the wall if you're slow enough, but if you're too fast then it will stop adding force (hopefully meaning it wont be fast enough to skip over the wall).

    You'll have to repeat this for each direction due to the way you've written your initial movement script.

    Code (CSharp):
    1. //Only add force if there's no wall to our left
    2. if (!Physics.Raycast(transform.position, Vector3.Left, rb.velocity.magnitude + force_x)
    3. {
    4.    //Use existing addforce line to move player left
    5. }
    Edit: I just realised you're using ForceMode.VelocityChange which might have been the issue, but I can see how it provides snappier movement. Hopefully the raycast check will still resolve the issue.
     
    Last edited: Oct 3, 2018
    lucadandois likes this.
  10. lucadandois

    lucadandois

    Joined:
    Sep 11, 2018
    Posts:
    12
    I'll definitely try this out tomorrow as I'm not at my pc right now. I've been messing around with the script today with different methods like rb.Transate() but then it still wanted to go trough the walls and I had to bump up the speed to 50 f in the original script because I added * Time.deltaTime. I'll send a new picture of the script as soon as I can.
     
    Serinx likes this.
  11. lucadandois

    lucadandois

    Joined:
    Sep 11, 2018
    Posts:
    12
    this is the current script with the raycast changes

    upload_2018-10-4_17-55-47.png
     
  12. lucadandois

    lucadandois

    Joined:
    Sep 11, 2018
    Posts:
    12
    euhm small error
    upload_2018-10-4_18-0-3.png
    upload_2018-10-4_18-0-13.png
     
  13. lucadandois

    lucadandois

    Joined:
    Sep 11, 2018
    Posts:
    12
  14. lucadandois

    lucadandois

    Joined:
    Sep 11, 2018
    Posts:
    12
    upload_2018-10-4_18-4-13.png
     
    Serinx likes this.
  15. Serinx

    Serinx

    Joined:
    Mar 31, 2014
    Posts:
    788
    Oh woops sorry I never actually tested it. So it worked?
     
  16. lucadandois

    lucadandois

    Joined:
    Sep 11, 2018
    Posts:
    12
    I had to delete the time. Deltatime part and I changed the speed to 5f. It worked in level one but the player won't move in level2
     
  17. lucadandois

    lucadandois

    Joined:
    Sep 11, 2018
    Posts:
    12
    ok i fixed it by adding two new floats and using them for the raycast instead of the Force_X float

    upload_2018-10-5_16-44-26.png
     
  18. techtycho

    techtycho

    Joined:
    Sep 10, 2020
    Posts:
    4
    Me too, I made a level to my sci-fi parkour game with pro-builder. The player keeps glitching through the walls when I enter a vertical and horizontal keyboard input at the same time. The player can move in the corner of the wall too. This is something that you don't want in your game except if you are making a game about the backrooms. I tried the .normalized but it didn't work. I also made another Vector3 variable that contains the horizontal input in the x and vertical input in the z. Then I normalized it. The first problem worked and I can no longer glitch through walls, But I can still walk through the corners. It also made the movement a bit snappy, As if you are using GetAxisRaw() instead of GetAxis(). I know some ways to fix that problem. But I am too lazy to add them in my game. So I am now searching for a cheap way to solve this problem. But your problem can be fixed by adjusting the z axis, or go to the player rigidbody and go to constraints, Then freeze the z position. You can also use character controller instead since it doesn't glitch through walls.