This is my first time ever using unity or C# so please be patient hahaha…
I’m trying to change the transparency/alpha of this table when the player collides with it, so you can see the player under the table. (I’m learning through the Unity Essentials tutorial if u can’t already tell)
This is what I’ve come up with so far, but I don’t know where to go from here…
I want to make public variables so I can change the strength of the alpha and the speed in which it fades in/out.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TableTransparency : MonoBehaviour
{
public float alpha = 1.0f;
public float fadeSpeed = 0.1f;
private void OnTriggerEnter2D(Collider2D other)
{
if (other.GetComponent<PlayerController2D>() != null)
{
transform.GetComponent<SpriteRenderer>().color -= new Color(0, 0, 0, fadeSpeed * Time.deltaTime);
}
}
}
update:
I still haven’t figured out how to fade, but I was able to make the alpha change! Except, I don’t know how to make it go back to normal when it’s exited.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TableTransparency : MonoBehaviour
{
public float alpha = 1.0f;
private void OnTriggerEnter2D(Collider2D other)
{
if (other.GetComponent<PlayerController2D>() != null)
{
transform.GetComponent<SpriteRenderer>().color -= new Color(0, 0, 0, alpha);
}
}
}
thanks so much! i had just come to that conclusion when you sent this message haha
the only thing i had to change in the code to get it to work was this: