按照需求,由于要模拟丧尸被击中的效果,不能使用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. ThinkPHP数据库访问CRUD;__SELF__和__ACTION__的区别;自动收集表单:$n->create();

    一.tp框架数据访问(pdo基础) public function test() { $n = D("Nation"); //select();find(); //查询 1.$at ...

  2. WCF Security(转载)

    WCF Security 主要包括 "Transfer Security"."Access Control"."Auditing" 几个部分 ...

  3. c# json TO xml

    using System.IO;using System.Text;using System.Xml.Serialization;using System.Xml;using System.Runti ...

  4. Spring之Ioc

    Spring的特性 >> 轻量级(Lightweight)相较于EJB而言Spring是轻量级的容器,不依赖任何web容器 >> 容器(Container) Spring本身不 ...

  5. Selenium2学习-039-WebUI自动化实战实例-文件上传下载

    通常在 WebUI 自动化测试过程中必然会涉及到文件上传的自动化测试需求,而开发在进行相应的技术实现是不同的,粗略可划分为两类:input标签类(类型为file)和非input标签类(例如:div.a ...

  6. SpringBoot RESTful 应用中的异常处理小结

    转载:https://segmentfault.com/a/1190000006749441 @ControllerAdvice 和 @ExceptionHandler 的区别 ExceptionHa ...

  7. SQLite - TRUNCATE TABLE

    https://www.tutorialspoint.com/sqlite/sqlite_truncate_table.htm Unfortunately, no TRUNCATE TABLE in ...

  8. 解决一阻塞语句CPU直降15%

    原本只是部署作业获取数据库中阻塞语句,中午测试汇集阻塞数据,发现某一服务器写入386行,而其他服务器只写入几行.登录对应服务器查看详细信息,发现有四个时间点分别写入100来行记录对于第一行:会话183 ...

  9. HDU 5063 Operation the Sequence(仔细审题)

    http://acm.hdu.edu.cn/showproblem.php?pid=5063 题目大意: 题目意思还是比较简单.所以就不多少了.注意这句话,对解题有帮助. Type4: Q i que ...

  10. C/C++ 结构体 数组 函数传递

    #include <stdio.h> #include <stdlib.h> struct student{ int num; ]; double dec; }; void s ...