按照需求,由于要模拟丧尸被击中的效果,不能使用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. vmware中两台虚拟机互相访问

    hosts文件   简单说,就是本来访问某个机器是通过其ip,在hosts文件中给ip对应一个名字,那么就可以通过名字来替代ip去访问该机器了(名字显然比ip好记) 环境:宿主机win10系统,安装了 ...

  2. Xshell远程连接工具

    下载地址:http://rj.baidu.com/soft/detail/15201.html?ald Xshell 是一个强大的安全终端模拟软件,它支持SSH1, SSH2, 以及Microsoft ...

  3. PHP和HTML代码混合编译的三种方法

    第一种是在HTML中加PHP. 大段大段的html代码中,在各个需要执行php的地方<?php .... ?> 比如 line7-9: 1 <head> 2 <meta ...

  4. redis笔记

    redis字符串 : 存储基本的一个键值对. redis哈希 : Redis的哈希值是字符串字段和字符串值之间的映射,所以他们是表示对象的完美数据类型. 一个哈希表可以存在多个键值对,可对键值进行增删 ...

  5. android逆向代码分析截图

  6. xcode中使用正则表达式来搜索替换代码

    有这样的需求: 类似于 GLOBAL_STR(@"请继续添加"); 这样的代码,需要批量修改为: GLOBAL_STR(@"请继续添加", nil); 这里使用 ...

  7. selenium RC+JAVA 笔记 一

    selenium 常用操作有:open,type,click,select,selectFrame. package com.example.tests; import com.thoughtwork ...

  8. TCP协议学习笔记(一)首部以及TCP的三次握手连接四次挥手断开

    TCP协议是一种面向连接的.可靠的流协议. 流即不间断的数据结构.这样能够保证接收到数据顺序与发送相同.但是犹如数据间没有间隔,因此在TCP通信中,发送端应用可以在自己所要发送的消息中设置一个标示长度 ...

  9. 去除magento多店铺URL地址中的“___from_store=”

    magento 的多店铺功能,大多数情况下是根据语言来进行选择的,当添加了多店铺之后,一般情况下我们会选择开启添加store code到url地址中. Magento 自带的这种功能算是比较不错了,但 ...

  10. shell中{}的妙用

    shell中${}的妙用   1. 截断功能 ${file#*/}:       拿掉第一条/及其左边的字符串:dir1/dir2/dir3/my.file.txt ${file##*/}:    拿 ...