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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Preventing player from going through walls (maze)?

Discussion in 'Scripting' started by loco_obito, Jun 16, 2022.

  1. loco_obito

    loco_obito

    Joined:
    Jun 16, 2022
    Posts:
    2
    Hi,
    first of all, I didn't know where exactly to put this thread but I figured it has something to do with scripting.

    i built a maze in cinema 4d and unified every wall into one game object. So the whole maze is now one object.

    I imported it into unity and i don't want my character to walk through the walls. This worked the first time through fixedUpdate while putting BoxColliders on individual Walls. But now I got this biiig maze and using a boxcollider is not possible now.

    I was thinking of using an if-method kinda like this. So it looks if it touches a wall and then doesn't move farther but this would require the maze to have a collider, right?

    if(collision.gameObject.CompareTag("Walls"))
    {
    ....
    }

    I'm now thinking of putting a box collider on every single wall but this would take forever. Is there a simpler method?

    Sry I'm new to scripting and unity and I'm feeling a little lost.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    There's lots of different kinds of colliders in Unity. One you have discovered is BoxCollider.

    Another is MeshCollider, which lets you use any arbitrarily shaped mesh as a collider, as its name would imply.

    Have you tried just adding a MeshCollider to your maze object in the scene? It might just work.

    Also, for this:

    You MUST spell and capitalize these words correctly, otherwise Unity doesn't know them.

    The correct capitalization in this case is
    FixedUpdate()


    If you ever doubt the capitalization, go STRAIGHT to the documentation.
     
  3. loco_obito

    loco_obito

    Joined:
    Jun 16, 2022
    Posts:
    2
    Ahh it worked! Thank you!
    Yes I did look out for correct spelling, I just didn't write it correctly in this thread.
     
    Kurt-Dekker likes this.