Search Unity

OnCollisionEnter

Discussion in 'Scripting' started by Cobeli, Aug 4, 2015.

  1. Cobeli

    Cobeli

    Joined:
    Jul 31, 2015
    Posts:
    3
    Hi everyone,

    So this is it, i am kind of new with Unity, and 2 friends and i we're making a 2D Platformer Game, our first one project and we really need help with something..

    We want to make the player can detect a colision with a wall at the same time a colission withing a another wall is False (off) by pressing a key. We think that InCollisionEnter method could work, but we don't know how to make that happend.

    Here is an example about what we want to do
     
  2. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Have you looked at the unity tutorials, they will help explain everything common that you will need.

    Remember if you are using 2d stuff then you need to use the 2d collision & trigger detection.

    Have a bool on your player that is set by the button press & have tags on your walls etc then try using nested if/if else statements to check all the combos & do what you want with each combo
     
  3. Gerald Tyler

    Gerald Tyler

    Joined:
    Jul 10, 2015
    Posts:
    80
    Here's some code I have from another project, I think this is what you're looking for

    So you'll need to add "Layers." Go to any game object and in the upper right look for Layers. Then click on the drop down box and go to Add Layers to create your own custom layers. So add a Light layer and a Dark layer. The following code will be set up where Light is layer 8, and Dark is layer 9.

    Next, go to Edit > Project Settings > Physics 2D.
    There you can set which layers will collide with each other.
    So set Light to collide with everything except Dark.
    Then set Dark to collide with everything except Light.

    Code (csharp):
    1.  
    2. public bool isLight = false;
    3.  
    4. void Update ()
    5. {
    6. if (Input.GetKeyDown ("tab"))
    7.   {
    8. if (isLight != true)
    9. {
    10. isLight = true;
    11. gameObject.layer = 8; //This causes the player's collision to align to whatever is on layer 8
    12. Debug.Log ("You are now Light");
    13. }
    14. else
    15. {
    16. isLight = false;
    17. gameObject.layer = 9; //This causes the player's collision to align to whatever is on layer 9
    18. Debug.Log ("You are now Dark");
    19. }
    20. }
    21. }
    22.  
    Let me know how that works out for you :)
    Also in the future if you could state a specific time to look at in a video, it would be helpful to those trying to assist you.
     
    Last edited: Aug 5, 2015
    Cobeli and tedthebug like this.
  4. Cobeli

    Cobeli

    Joined:
    Jul 31, 2015
    Posts:
    3
    Ok, thank you very much im gonna try that and i let you know about it.

    I really apreciate your help :)
     
  5. Cobeli

    Cobeli

    Joined:
    Jul 31, 2015
    Posts:
    3
    Hi Gerarld, thank you very much for the code i apreciate your help, it work perfectly :)

    Do you know how i can make the platform a little bit transparent, maybe with a 50% opacity when it is triggered?
     
  6. Gerald Tyler

    Gerald Tyler

    Joined:
    Jul 10, 2015
    Posts:
    80
    Sorry, I'm not familiar with that.