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();
c2D = gameObject.GetComponent();
}
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.