unity如何播放父级的父级物体所绑定的动画功能

using UnityEngine;

using System.Collections;
[RequireComponent (typeof (AudioSource))]


public class TargetCollision : MonoBehaviour {
private bool beenHit = false;
private Animation targetRoot;
public AudioClip hitSound;
public AudioClip resetSound;
public float resetTime = 3.0f;


// Use this for initialization
void Start () {
//试验多次,只有这种方法可以正确的返回父级的父级的动画资源
targetRoot = gameObject.transform.parent.GetComponentInParent<Animation> ();
Debug.Log (gameObject.transform.parent);
}

// Update is called once per frame
void Update () {

}
void OnCollisionEnter(Collision theObject){
//靶子的碰撞检测
if (beenHit == false && theObject.gameObject.name == "coconut") {
StartCoroutine ("targetHit");
}
}
IEnumerator targetHit(){
//碰撞检测时播放声音
GetComponent<AudioSource> ().PlayOneShot (hitSound);
//碰撞发生时播放动画
targetRoot.Play ("down");

beenHit = true;
//等待一个固定时间的另一个简易方法,因为在update里很耗资源
yield return new WaitForSeconds (resetTime);
//等待时间过后播放声音
GetComponent<AudioSource> ().PlayOneShot (resetSound);
//等待时间过后播放动画
targetRoot.Play ("up");
beenHit = false;
}


}

你可能感兴趣的:(图片,C#,脚本,unity,播放动画)