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

Making a sprite and collider desipere on collision and back on exit

Discussion in 'Scripting' started by Knude13, Dec 8, 2019.

  1. Knude13

    Knude13

    Joined:
    Oct 30, 2019
    Posts:
    2
    Hey guys. Im trying to make so if my player runs behind a hill e.g then it will turn down the transparency to 50% and up to 100% again when it leaves the hill obejct. the game is in 2D.

    Here is my script (Don't mind the comments they are on danish):

    public class FrontObjectDesipereTest : MonoBehaviour
    {

    private SpriteRenderer sR;
    private Collider2D c2D;


    void Awake()
    {
    sR = gameObject.GetComponent<SpriteRenderer>();
    c2D = gameObject.GetComponent<Collider2D>();
    }

    public void OnTriggerEnter2D(Collider2D bakke)
    {
    if (bakke.gameObject.tag == "FrontObjects")
    {
    sR.color = new Color(1f, 1f, 1f, 0.5f);
    c2D.enabled = true;
    }

    else
    {
    sR.enabled = false;
    c2D.enabled = false;
    }
    }

    public void OnTriggerExit2D(Collider2D bakke)
    {
    if (bakke.gameObject.tag == "FrontObjects")
    {
    sR.color = new Color(1f, 1f, 1f, 1f);
    c2D.enabled = true;
    }

    else
    {
    sR.enabled = false;
    c2D.enabled = false;
    }
    }
    }

    The hill got the tag "Frontobjects" and the script is on the hill and the trigger is on a box collider 2D on my player.
     
  2. Knude13

    Knude13

    Joined:
    Oct 30, 2019
    Posts:
    2
    at this point the hill desiperes on collision.