按照需求,由于要模拟丧尸被击中的效果,不能使用CharactorControll组件,只能使用rigidbody组件。

首先在场景上摆好僵尸和相机的位置,这里就不给相机加脚本了,直接固定住。

然后给丧尸加上了胶囊型的碰撞盒,用来检测鼠标的点击,当然刚体组件是不可少的。

随后就是控制丧尸的脚本,由于题目需求,将注释全部用英文写了,不过应该不难看懂。

首先设定不同的数值并初始化:

    public float move_speed=10f;
public float force_x=0f,force_y=200f,force_z=200f;
Transform m_transform,m_camera,blood;
Animation m_animation;
Rigidbody m_rigidbody;
string[] clipName={"idle","crawl","shamble","run","fallBack","hit1","hit2"};
bool is_hit=false,is_die=false;
public int hp=;//Zombie's hp,when 0 zombie die
// Use this for initialization
void Start () {
m_camera = GameObject.FindGameObjectWithTag ("MainCamera").transform;
m_transform = this.transform;
m_animation = m_transform.GetComponent<Animation> ();
m_rigidbody=m_transform.GetComponent<Rigidbody> ();
blood=Resources.Load("partice/blood_out",typeof(Transform)) as Transform;
}

然后要根据丧尸的移动速度给予丧尸不同的动画,理论上这里用动画状态机来做会更好,这里就是简单的切换播放:

       if (move_speed < 0.001f)
m_animation.Play (clipName []);
else if (move_speed < 1f)
m_animation.Play (clipName []);
else if (move_speed < 3f)
m_animation.Play (clipName []);
else
m_animation.Play (clipName []);

然后让丧尸面向相机并向前移动:

        m_transform.LookAt (m_camera);
m_rigidbody.transform.position+=(m_camera.position-m_transform.position).normalized*Time.deltaTime*move_speed;//zombie lookat camera and move to it

最后是点击丧尸时给予丧尸伤害,并且模拟一个击飞:

    void OnMouseDown(){
if (!is_die) {
m_rigidbody.AddForce (new Vector3 (force_x, force_y, force_z));//add a force if zombie not dead
StartCoroutine (blood_out ());//play the blood animator
hp--;
if ( == hp)
StartCoroutine (die ());
else
StartCoroutine (hit ());
}
}

上面的blood_out是我用粒子系统做的一个很简陋的动画,就不放出来了。

还有一些控制动画用的协程:

    IEnumerator die(){
is_die = true;
yield return StartCoroutine(playanimation(clipName[],1f));
yield return new WaitForSeconds(3f);//after 3 seconds,destory the dead zombie
Destroy(m_transform.gameObject);
}
IEnumerator hit(){
is_hit = true;
yield return StartCoroutine(playanimation(clipName[],0.2f));
is_hit = false;
}
IEnumerator playanimation(string name,float time){
m_animation.Play (name);
yield return new WaitForSeconds(time);
}

整个完整的脚本:

using UnityEngine;
using System.Collections; public class ZombieControll : MonoBehaviour {
public float move_speed=10f;
public float force_x=0f,force_y=200f,force_z=200f;
Transform m_transform,m_camera,blood;
Animation m_animation;
Rigidbody m_rigidbody;
string[] clipName={"idle","crawl","shamble","run","fallBack","hit1","hit2"};
bool is_hit=false,is_die=false;
public int hp=;//Zombie's hp,when 0 zombie die
// Use this for initialization
void Start () {
m_camera = GameObject.FindGameObjectWithTag ("MainCamera").transform;
m_transform = this.transform;
m_animation = m_transform.GetComponent<Animation> ();
m_rigidbody=m_transform.GetComponent<Rigidbody> ();
blood=Resources.Load("partice/blood_out",typeof(Transform)) as Transform;
} // Update is called once per frame
void Update () {
if (hp > && !is_hit) {
if (move_speed < 0.001f)
m_animation.Play (clipName []);
else if (move_speed < 1f)
m_animation.Play (clipName []);
else if (move_speed < 3f)
m_animation.Play (clipName []);
else
m_animation.Play (clipName []);//the animator depend on the move_speed
m_transform.LookAt (m_camera);
m_rigidbody.transform.position+=(m_camera.position-m_transform.position).normalized*Time.deltaTime*move_speed;//zombie lookat camera and move to it
}
}
void OnMouseDown(){
if (!is_die) {
m_rigidbody.AddForce (new Vector3 (force_x, force_y, force_z));//add a force if zombie not dead
StartCoroutine (blood_out ());//play the blood animator
hp--;
if ( == hp)
StartCoroutine (die ());
else
StartCoroutine (hit ());
}
}
IEnumerator blood_out(){
Transform t=Instantiate (blood).transform;
t.position = m_transform.position;
Debug.Log (t.name);
yield return new WaitForSeconds(1f);//the animator last for 1 seconds
Destroy(t.gameObject);
}
IEnumerator die(){
is_die = true;
yield return StartCoroutine(playanimation(clipName[],1f));
yield return new WaitForSeconds(3f);//after 3 seconds,destory the dead zombie
Destroy(m_transform.gameObject);
}
IEnumerator hit(){
is_hit = true;
yield return StartCoroutine(playanimation(clipName[],0.2f));
is_hit = false;
}
IEnumerator playanimation(string name,float time){
m_animation.Play (name);
yield return new WaitForSeconds(time);
}
}

总结一下今天做的unity面试题(一):刚体的点击事件的更多相关文章

  1. Unity 按空格一直触发Button点击事件的问题

    #解决 这是由于Button中Navigation(导航)功能导致的. 将导航设置为None即可. 真是气死我了,我说为什么点击完按钮界面,按空格就一直触发界面,难搞

  2. 史上最全的Unity面试题(持续更新总结。。。。。。) 包含答案的Unity面试题

    这个是我刚刚整理出的Unity面试题,为了帮助大家面试,同时帮助大家更好地复习Unity知识点,如果大家发现有什么错误,(包括错别字和知识点),或者发现哪里描述的不清晰,请在下面留言,我会重新更新,希 ...

  3. Unity 3D物体的点击事件响应以及NGUI坐标和世界坐标的互相转换

    Unity 版本:4.5 NGUI版本:3.6.5 参考链接:http://game.ceeger.com/Script/Camera/Camera.ScreenPointToRay.html,Uni ...

  4. Unity 4.6 uGUI的点击事件

    因为Unity 4.6刚刚发布,自带的uGUI功能的相关资料还不是很完善,今天刚装的Unity 4.6,想看一下uGUI是否好用,那么开始就今天的学习吧啊! 1,新建一个空的工程.

  5. Unity UGUI按钮添加点击事件

    1. 可视化创建及事件绑定 # 1 : 通过 Hierarchy 面板创建 UI > Button. 2 : 创建一个脚本 TestClick.cs, 定义了一个 Click 的 public ...

  6. 【web前端面试题整理05】做几道前端面试题休息休息吧

    前言 连续学了两天javascript的东西了,我们都累了,于是今天还是上一套面试题吧,大家一起休息休息,也为下个星期可能会有的面试准备下. 题目一览 CSS1.  overflow-x  属于 CS ...

  7. 【web前端面试题整理01】各位加班累了吧,来做点前端面试题吧

    前言 最近小叶子有点疲惫,主要是在外地工作生活上不太适应,吃一样的东西,我居然会拉肚子,而且是一个星期一个星期的.... 脸上长了一个豆豆一个星期还没消,我那个去啊. 昨天上午上班后,本来想继续研究j ...

  8. unity 面试题(答案)

    一.什么是渲染管道?是指在显示器上为了显示出图像而经过的一系列必要操作.渲染管道中的很多步骤,都要将几何物体从一个坐标系中变换到另一个坐标系中去.主要步骤有:本地坐标->视图坐标->背面裁 ...

  9. Unity 面试题

    一:什么是协同程序? 在主线程运行的同时开启另一段逻辑处理,来协助当前程序的执行,协程很像多线程,但是不是多线程,Unity的协程实在每帧结束之后去检测yield的条件是否满足. 二:Unity3d中 ...

随机推荐

  1. sort a Python dictionary by value

    首先要明确一点,Python的dict本身是不能被sort的,更明确地表达应该是"将一个dict通过操作转化为value有序的列表" 有以下几种方法: 1. import oper ...

  2. 线性时间O(n)内求数组中第k大小的数

    --本文为博主原创,转载请注明出处 因为最近做的WSN(wireless sensor network)实验要求用3个传感器节点接受2000个包的数据并算出一些统计量,其中就有算出中位数这么一个要求, ...

  3. HTML5web存储之localStorage

    localStorage与cookie的作用类似,只能存储字符串,以键值对的方式进行存储:与cookie不同的是,可以存储更多的数据. localStorage用于持久化的本地存储. var skey ...

  4. Sass与Web组件化相关的功能

    Sass https://en.wikipedia.org/wiki/Sass_(stylesheet_language) Sass (Syntactically Awesome Stylesheet ...

  5. 基于Java Mina 通信框架的JT/T809转发服务器设计

    Apache MINA 是 Apache 组织的一个开源项目,为开发高性能和高可用性的网络应用程序提供了非常便利的框架. 也是Java开发者的一个福利(.NET目前还没有类似封装的这么好的基础sock ...

  6. C#利用AxImp工具在WPF中使用OCX控件

    一.注册OCX并利用工具生成dll @echo off color a ::Failed REGSVR32 /S /I "MSCOMCTL.OCX" if exist %windi ...

  7. gulp外挂 uglify 的使用

    1.js文件压缩 第一步:安装外挂 :  第二步:gulpfile.js 配置 : (首先看你的package.json 中有没有添加依赖,如果有 这一句,代表添加成功啦.) 输入以下代码 : var ...

  8. Linux Bond的原理及其不足

    http://www.tektea.com/archives/1969.html. 在企业及电信Linux服务器环境上,网络配置都会使用Bonding技术做网口硬件层面的冗余,防止单个网口应用的单点故 ...

  9. phpstorm git no changes detected

    没有检测到的原因是符号链接了另一个 git 导致有两个git,这个时候应该排除掉其中一个.

  10. c#序列化json字符串及处理

    上面提到的第四篇文章最后有个解析数组的例子,出现了 .First.First.First.First.Children(); 我表示很晕,网上找的的例子大多数是关于JObject的,但是我很少看到JA ...