Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Wall Collision Not Working

Discussion in 'Physics' started by youchoosewhodies, Nov 5, 2022.

Thread Status:
Not open for further replies.
  1. youchoosewhodies

    youchoosewhodies

    Joined:
    Sep 2, 2022
    Posts:
    1
    So basically I had to take off the mesh renderer for the sprite renderer for my player object. I've given the player and the walls a box collider but they can still walk through walls. Is there something I'm missing or doing wrong? Or is it impossible to properly detect collision without a mesh renderer?

    Note: I'm really really new to Unity, I've been learning through Unity's main tutorials. So I may be missing something really simple.
     

    Attached Files:

  2. robertjulian027

    robertjulian027

    Joined:
    Nov 10, 2022
    Posts:
    1
    I guess the problem is that BOTH if blocks are executed if the player collides with the wall from the left. Let's have a detailed look at that scenario (colliding with a wall with velocityX > 0):

    Code (CSharp):
    1. private void checkIsOnWall()
    2. {
    3.     Actor wall = getOneIntersectingObject(VerticalWall.class);
    4.     if (wall != null)
    5.     {
    6.         if (velocityX >= 0)
    7.         {
    8.             velocityX = 0;
    9.             setLocation(wall.getX()-getImage().getWidth()+15, getY());
    10.         }//this part doesn't work
    11.         if (velocityX <= 0)
    12.         {
    13.             velocityX = 0;
    14.             setLocation(wall.getX()+getImage().getWidth()-15, getY());
    15.         }
    16.     }
    17. }
    - (line 6) first, the program enters the if-block for condition "velocityX >= 0".
    - (line 8) velocityX is set to 0.
    - (line 11) since velocityX is now 0, the second if-block is entered.
    - (line 14) the position of the player is set to "right of the wall".

    There you see the problem. There are twothree easy possibilities to fix this:
    1. Just add an "else" in front of the second if -> "else if (velocityX <= 0)" This way you can be sure that only one of the two if-blocks are executed for any situation.
    2. Does it really make sense to enter any of the two if-blocks if velocityX is 0? If you only collide with a wall because of the player moving, this should not be possible. In that case, you could change the if-conditions to "if (velocityX > 0)" and "if (velocityX < 0)". If on the other hand, the game also sees the possibility that a player gets somehow thrown into the wall without normal movement ("teleportation") and you want to make sure that the player immeditately gets thrown out of the wall, then solution possibility 1 above is your choice.
    Edit: 3. danpost was faster than me again. So using his solution is another easy possibility to fix it. ;) Note: Where does the "15" come from? Would this value still make sense if the image width would be different?
     
  3. johnsmit002

    johnsmit002

    Joined:
    Jan 6, 2023
    Posts:
    1
    I tried the above solution but its not working.

    Code (CSharp):
    1. private void checkIsOnWall()
    2. {
    3.     Actor wall = getOneIntersectingObject(VerticalWall.class);
    4.     if (wall != null)
    5.     {
    6.         if (velocityX >= 0)
    7.         {
    8.             velocityX = 0;
    9.             setLocation(wall.getX()-getImage().getWidth()+15, getY());
    10.         }//this part doesn't work
    11.         if (velocityX <= 0)
    12.         {
    13.             velocityX = 0;
    14.             setLocation(wall.getX()+getImage().getWidth()-15, getY());
    15.         }
    16.     }
    17. }
     
Thread Status:
Not open for further replies.