Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Animated Sprite Mask

Discussion in '2D' started by jy_erwewrwrew, Apr 1, 2021.

  1. jy_erwewrwrew

    jy_erwewrwrew

    Joined:
    Apr 21, 2020
    Posts:
    7
    So I have the animated sprite mask script. But the problem is when the animated gameobject finish the animation, the other instantiate object also stop too or went it is on loop it loop together too at the same time. I thought the animation will play when everytime it is spawn.So do I fix this?



    here is the code for the animated sprite mask
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class anotherscript : MonoBehaviour
    6. {
    7.     // The mask you want to animate
    8.     public SpriteMask mask;
    9.  
    10.     // The GameObject that has the animation
    11.     public SpriteRenderer targetRenderer;
    12.  
    13.     // It's important to use LateUpdate.
    14.     // If you use Update, the sprite mask will be 1 frame behind.
    15.     void LateUpdate()
    16.     {
    17.         if (mask.sprite != targetRenderer.sprite)
    18.         {
    19.             mask.sprite = targetRenderer.sprite;
    20.         }
    21.     }
    22. }



    here is the masking effect script like a scratch card
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ScratchCardEffect : MonoBehaviour
    6. {
    7.     public GameObject maskPrefab;
    8.    
    9.    public bool isPlayerInTrigger = false;
    10.     public GameObject blob;
    11.  
    12.     // Update is called once per frame
    13.     void Update()
    14.     {
    15.        
    16.  
    17.  
    18.         if(isPlayerInTrigger == true)
    19.         {
    20.             GameObject maskSprite = Instantiate(maskPrefab, blob.transform.position, Quaternion.identity)as GameObject;
    21.             maskSprite.transform.parent = gameObject.transform;
    22.         }
    23.         else
    24.         {
    25.  
    26.         }
    27.  
    28.        
    29.     }
    30.     private void OnTriggerEnter2D(Collider2D col)
    31.     {
    32.         if (col.CompareTag("Player")) //If it is the player.
    33.         {
    34.             isPlayerInTrigger = true;
    35.          
    36.         }
    37.     }
    38.  
    39.     private void OnTriggerExit2D(Collider2D col)
    40.     {
    41.         if (col.CompareTag("Player")) //If it is the player.
    42.         {
    43.             isPlayerInTrigger = false;
    44.         }
    45.     }
    46. }
     
  2. jy_erwewrwrew

    jy_erwewrwrew

    Joined:
    Apr 21, 2020
    Posts:
    7
    Hi I already solved the problem.