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. Dismiss Notice

2D Teleport animation

Discussion in 'Scripting' started by AngeloR2, May 14, 2021.

  1. AngeloR2

    AngeloR2

    Joined:
    Feb 24, 2021
    Posts:
    10
    I'm having a bit of trouble with this code for my animation. What I am trying to do is get my character to teleport. I have an animation that starts the teleportation and one that ends the teleportation that activates on a trigger. I've figured out how to get these to play but I haven't been able to move the distance at the right time. It takes about 2 seconds for my character to completely disappear. Right now when I press the key they move instantly but I want them to wait 2 seconds after the key is pressed to move. I was wondering if there was a couple of lines of code that would tell the animation to play after the 2 seconds have passed. Here is the code I have:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TeleportCharacterAnim : MonoBehaviour
    6. {
    7.     public Animator animator;
    8.  
    9.     // Update is called once per frame
    10.     void Update()
    11.     {
    12.         if (Input.GetKey(KeyCode.LeftShift))
    13.         {
    14.             animator.SetTrigger("TP");
    15.             float distance = 2;
    16.             transform.position = new Vector2(transform.position.x + distance, transform.position.y);
    17.         }
    18.     }
    19. }
    Animator Teleport.png
    "Teleport_T100_TP_Start" is the 2 second long animation I want to play before the movement. "Teleport-Code" is a few frames of my character completely invisible. "Teleport_T100_TP_End" is my character coming out of the animations and returning to their Idle.

    Additionally My character only moves in one direction when you press the button. Is there any way to get them to teleport in he direction they are facing?

    If anyone can help it would be much appreciated.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Problem 1: getting it to happen at the right time:

    Solution 1a: You can cheese this by simply starting a coroutine with a hard-wired delay equivalent to the animation time, THEN do line 16 above, rather than doing it instantaneously.

    Solution 1b: you could implement animation events and do the movement in there. More moving parts but then if you make the animation longer/shorter, it magically works, as long as you move the animation event

    Problem 2: teleporting based on facing direction.

    Solution 2: based on the whatever you are using to know facing direction, decide the destination position!
     
  3. AngeloR2

    AngeloR2

    Joined:
    Feb 24, 2021
    Posts:
    10
    Thank you. I like solution 1a: how would I write this out? I'm not too familiar with C#.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    This little CallAfterDelay script can do it for you. Look at the comment below the gist for an example of how to call it.

    https://gist.github.com/kurtdekker/0da9a9721c15bd3af1d2ced0a367e24e

    Beyond that you probably want to keep working through as many C# tutorials as you can to get comfy with it all.
     
  5. AngeloR2

    AngeloR2

    Joined:
    Feb 24, 2021
    Posts:
    10
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TeleportCharacterAnim : MonoBehaviour
    6. {
    7.     public Animator animator;
    8.  
    9.     void Start ()
    10.     {
    11.         animator = GetComponent<Animator>();
    12.     }
    13.     // Update is called once per frame
    14.     void Update()
    15.     {
    16.  
    17.         if (Input.GetKey(KeyCode.LeftShift))
    18.         {
    19.             animator.SetTrigger("TP");
    20.         }
    21.         if (animator.GetCurrentAnimatorStateInfo(0).IsName("Teleport-Code"))
    22.         {
    23.             animator.ResetTrigger("TP");
    24.             float distance = 1;
    25.             transform.position = new Vector2(transform.position.x + distance, transform.position.y);
    26.         }
    27.     }
    28. }
    I can't do more than this.