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

Design. Selective physics, walls, enemies, player. Pass through but detect trigger?

Discussion in 'Scripting' started by LeetRooster, Aug 26, 2013.

  1. LeetRooster

    LeetRooster

    Joined:
    Jul 20, 2013
    Posts:
    18
    I'm making a top down 2d game similar to Zelda: A Link to the Past.

    I'll have different types of objects
    Player
    Walls
    Enemies

    Here are some requirements.
    - The Player and Enemies can not pass through walls
    - The Player and Enemies should not block each others' movements.
    - When the Player and Enemies pass through each other, we should still be able to detect it (via trigger?) and apply damage and an animation.

    I think I might need layers to pull this off. I think what this means to unity is:
    - Create 3 layer for enemies, players and walls.
    - Only enable collisions between enemies and walls and players and walls.
    - Disable the collisions between the players and enemies, enemies and enemies, players and players.
    Now I'm wondering, with these restrictions will you still be able to detect a trigger? Is this even possible since you can only have one collider attached to a game object? Can you do some magic with adding an empty GameObject as a child with a collider?

    I'm not too sure if this fits into the problem, but in regards to these choices I'll explain what I have setup so far.
    - Static Collider
    - Rigidbody
    - Kinematic Rigidbodies
    - Character controller (Probably will avoid this due to capsule collider)

    What I have setup so far is:
    - Walls: Kinematic Rigidbody (I can move these when the player pushes them, the way link could push blocks in LTTP). Though I think I could get the same effect with static colliders. The player and enemies won't pass through them. I'm not sure if I need a rigidbody on these walls.
    - Player - RigidBody
    - Enemies - Rigidbody

    This prevents players and enemies from passing through walls, but they can collider with each other and push each other around. I really want a pass through effect but be able to detect when they pass through each other to apply damage. How do I make sure that the players and enemies don't block each other, but I can still detect that they hit each other?


    I move most things around in the game with rigidbody.MovePosition, but I'm experimented and gotten pretty much the same effect by calculating a change in velocity and adding forces to rigidBodies with velocity mode.

    So do you have any suggestions for me? Can I solve this with layers? Or will what I've described not work? Have I made good choices in regards to rigidBody vs kinematic rigidbody vs static collider.
     
  2. BlackMantis

    BlackMantis

    Joined:
    Feb 7, 2010
    Posts:
    1,475
    Your description of the problems your having is confusing. Post your scripts and knock one problem out at a time.

    Have you played with trigger functions yet ?

    Have you looked in the doc for Physics Ignore Collision examples ?
     
  3. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Have a 4th layer for trigger interactions, and each entity has a collider for moving and bumping into walls, AND a trigger for damage
     
  4. LeetRooster

    LeetRooster

    Joined:
    Jul 20, 2013
    Posts:
    18
    Thanks guys, the extra layer for the trigger is so obvious in hind sight. In case anyone else is wondering the relevant parts of the solution are:

    Enemies -> Two colliders (Add one child as a normal collider, and one child as a normal trigger. Probably want them the same size).
    ---Collider layer - Enemies
    ---Trigger layer - EnemiesTrigger (The 4th layer)
    ---Monobehaviour goes on the parent, it will get both events. You don't need the put the mono behaviour on the child. Though I think you can do this if it makes sense for your use case.

    Player -> Collider as Player layer
    Walls - Collider as Wall layer

    Collisions allowed for Enemy Layer:
    Walls

    Collisions allowed for Player Layer:
    Walls
    EnemyTrigger

    Collisions allowed for EnemiesTrigger Layer:
    Player

    Collisions allowed for Walls Layer
    Player
    Enemies

    And depending on your game maybe allow collisions between Player and Player (Or rather you would introduce a PlayerTrigger layer).