Search Unity

GameObject thats Solid but is also a Trigger

Discussion in 'Scripting' started by gretty, Sep 16, 2012.

  1. gretty

    gretty

    Joined:
    Jun 20, 2012
    Posts:
    72
    Hello

    Can a GameObject be solid(have the player not move through it) and still have collider.isTrigger set to true?

    I am trying to create a Super Mario Game. I am up to the stage of creating the bricks that mario hits with his head. The brick has a Box-Collider and has the isTrigger property set to true so it can detect when Mario collides with the brick and break the brick. Note the brock has a Box Collider.

    Problem: The bricks aren't solid, ie, Mario goes straight through the brick instead of stopping when his head hits the Brick gameObject.

    How can I make my Brick solid but still have the collider.isTrigger set?

    Code (csharp):
    1. public class BrickItem : MonoBehaviour {
    2.    
    3.     public enum State {BRICK_SOLID, BRICK_BREAKABLE, BRICK_ITEM};
    4.     public State state                = State.BRICK_SOLID;
    5.     public Material breakableMaterial = null;
    6.     public Material solidMaterial     = null;
    7.     public Material itemMaterial      = null;
    8.    
    9.     void Start () {
    10.         setState (state);
    11.     }
    12.    
    13.     void Update () {
    14.    
    15.     }
    16.    
    17.     void OnTriggerEnter(Collider collider) {
    18.     //void OnCollisionEnter(Collision collider) {
    19.        
    20.         Debug.Log("PRIOR");
    21.         if (collider.gameObject.name != "Mario")
    22.             return;
    23.        
    24.         Debug.Log("HITTT");
    25.         // TODO: play sound
    26.         switch (state)
    27.         {
    28.             case State.BRICK_ITEM:
    29.             {
    30.                 setState( State.BRICK_BREAKABLE );
    31.             }
    32.             break;
    33.             case State.BRICK_BREAKABLE:
    34.             {
    35.                 setState( State.BRICK_SOLID );
    36.             }
    37.             break;
    38.             //case State.BRICK_SOLID:
    39.             default:
    40.             {
    41.                
    42.             }
    43.             break;
    44.         }
    45.     }
    46. }
    47.  
    48.  
     
  2. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    5,041
    Why can't you set trigger=false and use the OnCollision Enter that is commented out...
     
  3. gretty

    gretty

    Joined:
    Jun 20, 2012
    Posts:
    72
    I've tried that but the OnCollisionEnter function doesn't get called when Mario hits the Brick?

    Note that the code above is attached to the Brick, the brick has a Box-Collider, no rigidbody and Mario has a CharacterController.

    How can I get the function to get called/trigger?
     
  4. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    5,041
    It's my understanding Character Controller(CC) doesn't play very well with others, personally I write my own as the CC doesn't give very good physics for anything other than a basic FPS (again as far as I can tell). Because of this I can't provide any kind of definitive advice on using CC. Some things that pop to mind ... use a trigger collider and slightly inside that a "standard" collider, or add a trigger collider as a child of your CC which you use to trigger events.
     
  5. gretty

    gretty

    Joined:
    Jun 20, 2012
    Posts:
    72
    Thanks, will keep trying to get it to work.

    PS: Your games PhRONIX and Vibre look awesome, did u make them on your own or part of a team? Do you ever find that you cant load too many textures(images) for your GUI/Game because iOS Android limit the amount of memory you can have?
     
  6. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Why not use both? Have two objects. One a trigger, one a colliision. Both exactly the same size and position.