- Home /
[Solved] How to know if Sprite Mask is showing a sprite or not via script
Hi all, what I want to achieve is to know when a sprite is visible in the game scene using a sprite mask.
OnBecomeVisible()
or renderer.isVisible
does not seem to work. They are called right away or always set to true even though I am not showing them using a sprite mask.
What I want to get is to show specific enemies with my cursor, kind of like the Lens of Thruth in the Legend of Zelda, and that I have achieved. What I wanted to do now is do some other stuff(play sounds, particle effects, etc) at the moment the sprite becomes visible using my "Lens of Thruth"
I don't find necessary to add any image or code since this question is pretty much straight forward, but if necessary I do it.
Any help with this will be much appreciated.
As an extra for this I have checked that the OnBecameVisible()
and renderer.isVisible
as the unity API says, they will be activate or set to true as long as they are in the Game View or the Scene view.
Once you have the sprites outside both of thos cameras the renderer.isVislble
is set to false. So know what I still haven't figure out is to know via script the moment a Sprite is visible using my LensOfThruth (Sprite $$anonymous$$ask). Still figuring out how to do that.
Answer by exploringunity · May 22, 2020 at 07:40 PM
Hey @CesarCanto,
Have you considered using colliders/triggers?
Below is an example test script that demonstrates the idea of using colliders to detect when the sprite mask has revealed a sprite. The first screenshot shows the scene setup and sprite mask ("magic spotlight") settings, and the second is the inspector for the sprite that is being revealed.
using UnityEngine;
public class MagicSpotlight : MonoBehaviour
{
Camera cam;
Transform camTransform;
Transform spotlightTransform;
void Start()
{
cam = Camera.main;
camTransform = cam.transform;
spotlightTransform = transform;
}
void Update()
{
var mousePos = Input.mousePosition;
mousePos.z = -camTransform.position.z;
var worldPos = cam.ScreenToWorldPoint(mousePos);
spotlightTransform.position = worldPos;
}
void OnTriggerEnter2D(Collider2D other)
{
Debug.Log($"Magic Spotlight revealed {other.gameObject.name}");
}
}
Hope this helps!
Thanks man! this was what I was looking for. using a Trigger as big as my sprite mask I could detect the enemy inside it and activate my events.
Also as a side note for this, this approach is for things that you can throw or manipulate in the game, but if you plan to use the mouse to drag a "Lens of Thruth" around the screen, you can also use a Trigger and call On$$anonymous$$ouseEnter()
inside the script attached to the object you want to interact with the sprite mask. Is basically the same approach and you don't have to use a RigidBody, but as it says it only works with the mouse.
Answer by frilanski · May 22, 2020 at 05:14 PM
How are you toggling whether it is visable? if you just enable and disable the sprite renderer then you should be able to just use the below:
if(renderer == true)
provided you're referencing the renderer correctly.
Hi frilanski, I don't want to enable/disable my Sprite Renderer I have been having some problems with that approach since the gameobject I want to "Appear/Disappear" is playing some animations, is better to use a mask so the Sprite Renderer can remain active and avoid problems with animations, at least in my case that was an issue.