- Home /
How can i check if an animation is done?,How to check if an animation is done?
I am using an animator with the only layer being one animation that only plays once and doesnt loop, as soon as the animation is done i would like the player2 to be sucked up into the space ship, i tried searching it but none of those answers worked so thats why im asking here. This is my code, but only look at the alien part since i havent worked on the astronaut yet.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class FirstToEnd : MonoBehaviour { [Header("Alien")] [SerializeField] GameObject AlienShip; [SerializeField] Vector2 AlienShipStart; [SerializeField] Vector3 AlienShipEnd; [SerializeField] GameObject Player2; bool AlienShipLerp = false; bool Player2Lerp = false; [SerializeField] Vector3 Offset = new Vector3(0, 200);
[Header("Astronaut")]
[SerializeField] GameObject AstronautBuilding;
[SerializeField] GameObject Tubes;
[SerializeField] GameObject Player1;
[Header("Lerp")]
[SerializeField] float TimeStartedLerping;
[SerializeField] float LerpTime;
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.transform.CompareTag("P1"))
{
GetTubes();
}
else if (collision.transform.CompareTag("P2"))
{
GetUfo();
}
GetComponent<BoxCollider2D>().enabled = false;
}
void GetTubes()
{
AstronautBuilding.SetActive(true);
Tubes.SetActive(true);
}
void GetUfo()
{
AlienShip.SetActive(true);
LerpUfo();
}
void LerpUfo()
{
TimeStartedLerping = Time.time;
AlienShipStart = AlienShip.transform.position;
AlienShipLerp = true;
Player2.GetComponent<Player2>().enabled = false;
Player2.GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezePosition;
}
private void Update()
{
if (AlienShipLerp)
{
AlienShipEnd = Player2.GetComponent<Transform>().position + Offset;
AlienShip.transform.position = Lerp(AlienShipStart, AlienShipEnd, TimeStartedLerping, LerpTime);
if (AlienShip.transform.position == AlienShipEnd)
{
AlienShip.GetComponent<Animator>().enabled = true;
if (!AlienShip.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).IsName("UfoAnimation"))
{
Invoke("UpPlayer", 0.01f);
TimeStartedLerping = Time.time;
AlienShipLerp = false;
}
}
}
if (Player2Lerp)
{
Player2.transform.position = Lerp(Player2.transform.position, Player2.transform.position + Offset, TimeStartedLerping, LerpTime);
if (Player2.transform.position == Player2.transform.position + Offset)
{
Player2.SetActive(false);
Player2Lerp = false;
}
}
}
Vector3 Lerp(Vector3 start, Vector3 end, float timestartedLerping, float lerpTime = 1)
{
float timeSinceStarted = Time.time - timestartedLerping;
float percentageComplete = timeSinceStarted / lerpTime;
var result = Vector3.Lerp(start, end, percentageComplete);
return result;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612225940im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
2D Animation does not start 1 Answer
Transform.Rotate keeps snapping back to original position after it has finished 0 Answers
What is the proper way to organize animator transitions to a large amount of abilities? 0 Answers
[4.6 UI] Image fading - How to? 2 Answers
Waiting for condition to be true for X amount of time before allowing transition in animator? 0 Answers