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;
}
}
相关文章推荐
- 【Unity开发】通过C#读取CSV表,创建并存储到LUA table中
- wiki/Cg Programming/Unity/Cutaways
- 增强现实贺卡项目展示
- [Unity3D]Unity3D游戏开发之鼠标滚轮实现放大缩小
- Unity-编写插件
- unity音频管理
- unity5.3多线程渲染的坑
- unity3d的四元数 Quaternion
- Unity3D项目之《坦克大战》
- Unity Assets目录下的文件夹用途
- Unity中加入Android项目的Build步骤 .<unity引用Android的jar包><Android对unity做二次开发>
- [Unity3D]蓝港面试题
- Unity3d 换装Avatar系统
- 项目记录25--unity-tolua框架 View02---BasePanel.lua
- Unity3D与android交互---构建android插件
- unity3d,android平台下,高德地图定位,定位模式设定
- Unity3D 调用 Android jar 包制作方法
- Unity 5 中的全局光照技术详解(建议收藏) - CocoaChina_让移动开发更简单
- unity脚本自带函数执行顺序
- unity3d 打包出错的问题