Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How to create fade animation when my character teleports?

Discussion in '2D' started by janjicm, May 15, 2020.

  1. janjicm

    janjicm

    Joined:
    Apr 29, 2020
    Posts:
    16
    Ok, so I tried doing this in the only way I know.

    Here is the script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Teleporting : MonoBehaviour
    6. {
    7.  
    8.     public Transform teleportTarget;
    9.     public GameObject thePlayer;
    10.  
    11.     public GameObject teleportUI;
    12.  
    13.  
    14.     void OnTriggerEnter2D(Collider2D other)
    15.     {
    16.         teleportUI.SetActive(true);
    17.         thePlayer.transform.position = teleportTarget.transform.position;
    18.     }
    19.  
    20. }
    21.  
    It basically turns on my panel which has fade animation and it works the first time. Problem is that after finishing this, my panel stays active. That means that animation won't play again because my panel is already active.

    Any solution?

    EDIT:
    I fixed it. I made new script that sits on panel: https://paste.myst.rs/n28
    after that you will select your animation and then:
    https://imgur.com/a/PjYvB1h
    1. Click on that to add AnimationEvent on your last frame
    2. Select ( I don't know how to call that ) but it's above your animation key
    3. Find your function that turns off panel
    That's it!
     
    Last edited: May 15, 2020
  2. Derekloffin

    Derekloffin

    Joined:
    Mar 14, 2018
    Posts:
    322
    Probably just add a timer of some kind that deactivates the teleportUI after a time expires.
    You can do that by setting a constant float for the timeout, on setting teleportUI to active, you set a tracking float variable to 0, then every update/fixed update you increment that float variable by the deltatime/fixeddeltatime and compare against your constant. If greater or equal, deactivate the teleportUI again and stop incrementing (although deactivating the teleportUI I think will suspend update/fixedupdate so that should be sufficient I think).
     
  3. janjicm

    janjicm

    Joined:
    Apr 29, 2020
    Posts:
    16
    I actually did it in another way, pretty much added AnimationEvent which calls function that turn's off panel at the end of animation