- Home /
How can i check if animation has finished playing if the object have no animator attached ?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PointAndClick : MonoBehaviour
{
public float moveSpeed = 50;
private GameObject thedoor;
private Animator anim;
void Start ()
{
thedoor = GameObject.FindWithTag("SF_Door");
anim = thedoor.GetComponent<Animator>();
}
void Update()
{
if (transform.position.z < 100)
transform.position += Vector3.forward * Time.deltaTime * moveSpeed;
if (anim.GetCurrentAnimatorStateInfo(0).IsName("open"))
{
string c = "check";
}
}
}
The problem is that the object door have no Animator component attached to it only Animation component. So i'm getting exception. Is there a way to figure out when animation finished playing without the Animator component ?
What i want to do is to idle the player while the animation is playing and move the player again when the animation is finished playing. This is the script i'm using to trigger the animations:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class door : MonoBehaviour
{
void OnTriggerEnter(Collider obj)
{
var thedoor = GameObject.FindWithTag("SF_Door");
thedoor.GetComponent< Animation > ().Play("open");
}
void OnTriggerExit(Collider obj)
{
var thedoor = GameObject.FindWithTag("SF_Door");
thedoor.GetComponent< Animation > ().Play("close");
}
}
Both animations are attached to the player. So when the player is entering and it's playing the open animation i want the player to be idle like waiting and once the door is opened(animation open finished playing) make the player move again.
Answer by Chocolade · Sep 18, 2017 at 11:46 PM
Found it by changing from GetComponent to GetComponent and then using the IsPlaying property like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PointAndClick : MonoBehaviour
{
public float moveSpeed = 50;
private GameObject thedoor;
private Animation anim;
// Use this for initialization
void Start ()
{
thedoor = GameObject.FindWithTag("SF_Door");
anim = thedoor.GetComponent<Animation>();
}
// Update is called once per frame
void Update()
{
if (!anim.IsPlaying("open"))
{
if (transform.position.z < 100)
transform.position += Vector3.forward * Time.deltaTime * moveSpeed;
}
}
}