Hi! Complete newbie here, so bear with me for a bit. I’m creating a stealth 2D game for school, and I’m wondering is there anyway to make it so that on trigger enter/stay, the game object the player is hiding in will fade? As well as on trigger stay the player is safe from enemy sighting.
I’ve been trying it out with different other answers on the forums but none of them have been working.
I want it to run off something like:
OnTriggerEnter2D(Collider other)
{ (if other.tag == “visionBlocker”)
{ GameObject.Find(“Enemy”).GetComponent(HurtPlayer).enabled = false; }
or something like that…?
As for the colour aspect I’ve been playing around with the Color32 values but it hasn’t been working out for me either.
I tried running something along the lines of:
under void start it would have:
sr = GetComponent();
and then
void OnTriggerEnter2D (Collider other) {
sr.Color = new Color32(255, 255, 255, 100); }
I think for the fading part you should check out this : Unity - Manual: Coroutines
Because they use fading as an example (and it’s a good example of what should be done i think)
Good luck !
Try not to use GetComponent() in places where you can cache the value to some variable. This is heavy function. And definitely don’t use GameObject.Find() in places where you code is invoked more than once (for example Awake() or Start()) and the best option is not to use it at all. It’s just bad practice. Same goes for FindObjectOfType FindObjectWithTag etc.
These functions are very heavy (GameObject.Find() need to go through every game object on your scene to find the one you’re looking for, imagine that you have thousands of objects on your scene and you are going through them every time OnTriggerEnter2D is invoked (in your case.)
Try using direct references through Inspector, or some stuff like Singletones, registering objects on Awake to some kinds of managers, etc.
Enablind, disabling components means that their Update() function is no more invoked. If you have some other functions like OnMouseDown() etc. - they will still work. Maybe this is the case. You would probably want to have some variable like CanHurtPlayer in your HurtPlayer component. And you’ll probably might want to use GetComponent() or GetComponent(typeof(HurtPlayer)) otherwise this code won’t compile.
For changing color - you might want to use Color class not Color32. It accepts values in constructor from range (float) [0, 1] not (int) [0, 255] this allows to have more colors than 255^3 (or 255^4 with alpha). This is much better if you’re dealing with fading.
And you’ll probably will need to use Coroutines as mentioned above. And Color class has also static function called Lerp() wich allows to fade beetween two values i.e. from Black to White.
I’m not sure where you’re invoking your second OnTriggerEnter2D() so I can’t tell if it is the problem. But you might want to check it. And if your triggers are not invoked at all remember that at least one of the objects needs to have Rigidbody2D (or Rigidbody for 3D physics) attached and enabled.