您的位置:首页 > 移动开发 > Unity3D

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

2016-03-03 11:41 1131 查看


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;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: