Search Unity

Hiding character

Discussion in '2D' started by VinniciusLeonk, Sep 18, 2017.

  1. VinniciusLeonk

    VinniciusLeonk

    Joined:
    Sep 18, 2017
    Posts:
    4
    Hi guys... Please, can you help me? I'm wondering about my script, how can I put my character in a bush to hidden it and take off the collider2D for the enemy dont detect it?!
     
    Last edited: Sep 18, 2017
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Probably not, without a more detailed question. But I'll try.

    I'm wondering about your script too, as you haven't shown it. :) When you do, please be sure to use the "Insert Code" button in the toolbar (located between the Movie icon and the floppy disk icon).

    Anyway, to put your character in a bush, just get the position of the bush, and assign your character's transform position to that, e.g.:

    Code (csharp):
    1.   transform.position = bush.position;
    (where 'bush' is a public field of type Transform, to which you have assigned the bush object).

    Code (csharp):
    1.   GetComponent<Collider2D>().enabled = false;
    This disables it, rather than actually removing it, but I think that's what you want.[/code]
     
    VinniciusLeonk likes this.
  3. VinniciusLeonk

    VinniciusLeonk

    Joined:
    Sep 18, 2017
    Posts:
    4
    So, here is my script...


    if(Input.GetKeyDown(KeyCode.C)){ // aparecer e desaparecer...

    rend.enabled = !rend.enabled;
    boxcol.enabled = !boxcol.enabled;

    //gameObject.activeSelf = !gameObject.activeSelf;
    //this.GetComponent<Collider2D>().enabled = false;
    //GetComponent<Collider2D>().enabled = !GetComponent<Collider2D>().enabled ;
    }
     
  4. Joimer

    Joimer

    Joined:
    Aug 1, 2017
    Posts:
    14
    You can use the collider with a trigger, then you can use OnTriggerEnter2D and OnTriggerExit2D to manage the events of entering and exiting the bush to change the state of the character.
     
  5. hlw

    hlw

    Joined:
    Aug 12, 2017
    Posts:
    250
    If the player collider has a specific layer, and if the ennemies detect it using another collider with also a specific layer, you can call.
    Physics2D.IgnoreLayerCollision(playerLayerId, ennemyLayerID, true),
    then set if to false again when going out of the bushes. Ennemies won't see the collider, but the collider will stay there and active (just in case something need to see the player even if hidden in the bush).
    You can also use Physics2D.IgnoreCollision(The_collider_of_the_player, The_collider_of_the_ennemy,true), bout you will have to do it for every ennemy, and then set it to false for every ennemy once you go out of the bush.

    If your ennemies use raycasts to detect the player, there also is some solutions.

    Also make sure, in your ennemy code, if when your ennemy detects the player it adds a reference to the player to track it, to set this reference to null when the ennemy stops detecting the player.

    You can change the color of your sprite renderer to new Color(0,0,0,0) when hidden, and set it back to Color.White when going out.
     
    paulusuluap likes this.