Search Unity

How can I prevent the player from moving through walls ?

Discussion in 'Physics' started by dubiduboni_unity, May 7, 2019.

  1. dubiduboni_unity

    dubiduboni_unity

    Joined:
    Feb 11, 2019
    Posts:
    116
    The problem is that the objects that make the space station are too many over 1000 objects. So it's hard to go one by one and add a collider to it.

    The player have a Rigodbody and I'm using it to detect when entering a door and collide with the door collider and then open the door/s .

    I marked with red circle one part of the space station for example that don't have any collider on it. So the player can move through it.

    This is the screenshot also of the player inspector settings :

     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,195
    You don't really have a lot of options here. Performance wise, you'll almost certainly want to go around placing simple colliders to block off the geometry where the player isn't allowed to go. Although your scene might look complex, a small number of invisible box colliders could be used to easily block the areas the player shouldn't be able to go. That's generally how things work. Ideally you're working with prefabs, and you've built this space station from a bunch of individual prefabs, in which case you could go add invisible box colliders to those objects.

    Otherwise, you could add mesh colliders, but just be aware that can be really expensive.
     
  3. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Another way is to have the walkable area mapped in code and every time you move the player you check if the movement is legal, sorta like the nav mesh works.

    I would just add colliders though.
     
    xVergilx and dubiduboni_unity like this.