- Home /
How to change color of sprite as it moves over different backgrounds
Hello,
I have a rectangle sprite which moves slides across two backgrounds, white and black, as shown in the image below. I would like to change the part of the rectangle sprite to opposite color when its on a particular background.
Example: when half of the sprite is on black background and other half on white, then the part which is on black background should change its color to white and the vice versa for the other side.
You will better understand my question with the images below.
Answer by MrRightclick · Jul 04, 2019 at 08:19 AM
Sprite Masks might be of use here. https://docs.unity3d.com/Manual/class-SpriteMask.html
Thank you for the reply, I will look into this.
Sorry for the late reply, its been a rough week.
I checked it and it perfectly solves my issue. Thanks again.
Answer by dbchest · Jul 13, 2019 at 07:18 AM
@mackz09 alternatively, you could attach and align several "Box Collider 2D" components to your background images and utilize the "OnTriggerEnter" methods via custom script from the Collider class. I have never used a sprite mask myself, but if you find that works better for you please share your experiences here so I can learn a thing or two about them! good luck!
Sprite masks are the perfect solution for this kind of situation. I also though about your answer, but having multiple box colliders can impact performance. Plus I have different and complex shapes, which might require tweaking the collider boundaries for each of those which again is time intensive. I would highly suggest you use Sprite $$anonymous$$asks, as they were designed for this type of scenario.
thank you very much for the insight! it is a good piece of information to have in the toolbox; I am glad you got everything working for you like you needed.