总结一下今天做的unity面试题(一):刚体的点击事件

按照需求,由于要模拟丧尸被击中的效果,不能使用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面试题(一):刚体的点击事件的更多相关文章
- Unity 按空格一直触发Button点击事件的问题
#解决 这是由于Button中Navigation(导航)功能导致的. 将导航设置为None即可. 真是气死我了,我说为什么点击完按钮界面,按空格就一直触发界面,难搞
- 史上最全的Unity面试题(持续更新总结。。。。。。) 包含答案的Unity面试题
这个是我刚刚整理出的Unity面试题,为了帮助大家面试,同时帮助大家更好地复习Unity知识点,如果大家发现有什么错误,(包括错别字和知识点),或者发现哪里描述的不清晰,请在下面留言,我会重新更新,希 ...
- Unity 3D物体的点击事件响应以及NGUI坐标和世界坐标的互相转换
Unity 版本:4.5 NGUI版本:3.6.5 参考链接:http://game.ceeger.com/Script/Camera/Camera.ScreenPointToRay.html,Uni ...
- Unity 4.6 uGUI的点击事件
因为Unity 4.6刚刚发布,自带的uGUI功能的相关资料还不是很完善,今天刚装的Unity 4.6,想看一下uGUI是否好用,那么开始就今天的学习吧啊! 1,新建一个空的工程.
- Unity UGUI按钮添加点击事件
1. 可视化创建及事件绑定 # 1 : 通过 Hierarchy 面板创建 UI > Button. 2 : 创建一个脚本 TestClick.cs, 定义了一个 Click 的 public ...
- 【web前端面试题整理05】做几道前端面试题休息休息吧
前言 连续学了两天javascript的东西了,我们都累了,于是今天还是上一套面试题吧,大家一起休息休息,也为下个星期可能会有的面试准备下. 题目一览 CSS1. overflow-x 属于 CS ...
- 【web前端面试题整理01】各位加班累了吧,来做点前端面试题吧
前言 最近小叶子有点疲惫,主要是在外地工作生活上不太适应,吃一样的东西,我居然会拉肚子,而且是一个星期一个星期的.... 脸上长了一个豆豆一个星期还没消,我那个去啊. 昨天上午上班后,本来想继续研究j ...
- unity 面试题(答案)
一.什么是渲染管道?是指在显示器上为了显示出图像而经过的一系列必要操作.渲染管道中的很多步骤,都要将几何物体从一个坐标系中变换到另一个坐标系中去.主要步骤有:本地坐标->视图坐标->背面裁 ...
- Unity 面试题
一:什么是协同程序? 在主线程运行的同时开启另一段逻辑处理,来协助当前程序的执行,协程很像多线程,但是不是多线程,Unity的协程实在每帧结束之后去检测yield的条件是否满足. 二:Unity3d中 ...
随机推荐
- JAVA基础篇NO2--Java中的基本命名规则及数据类型
1.Java中的常量及进制 1.常量: 在程序运行的过程中,不可以改变的量,就是常量 boolean类型的值只能是true或者false null: 空常量, 代表不存在! ------------- ...
- js正则匹配的一个日常应用
应用实例 1 /** 将段落中的 \n 转换为 <p></p>, 规范存储 */ 2 function formatParagraphForStore(val) { 3 var ...
- ECharts饼图试玩
处理类似提交问卷的数据,要生成图表,用了ECharts,好方便的. 简陋效果: 1.表单存储 有单选和多选题,单选直接存储各选项数字值,1,2,3,4...中一个:多选用|分隔存储选项值,如1|3,2 ...
- ACM集训的Training Day 3的A题。。。
A. 等差数列 一.题目描述: 一个等差数列是一个能表示成a, a+b, a+2b,..., a+nb (n=0,1,2,3,...)的数列. 在这个问题中a是一个非负的整数,b是正整数.写一个程序来 ...
- 使用css使textbox输入内容自动变大写
<style type="text/css"> input[type="text"] { text-transform:uppercase; } & ...
- LeetCode Minimum Moves to Equal Array Elements
原题链接在这里:https://leetcode.com/problems/minimum-moves-to-equal-array-elements/ 题目: Given a non-empty i ...
- vmware centos nat模式下连不上网络解决办法
简单来讲,当你创建一台虚拟机时,VMware为你虚拟了三种接入网络的方式:桥连接,NAT,使用主机网络,Vmware 10中默认对应 VMnet0,VMnet1,VMnet8 . 当选择桥连接方 ...
- CentOS下 pycharm开发环境搭建
经过一系统列的折腾之后,我终于有高版本的python和我熟悉的输入法用了,下面来搭建pycharm下的python开发环境. 1.首先安装java jdk注意是JAVA 的JDK,不是JAVA VM什 ...
- nexus
下载地址:http://pan.baidu.com/s/1nvwIoa9 (Jfrog/Nexus) maven 仓库: http://mvnrepository.com/ 用户名密码分别是:ad ...
- linux下设置固定IP
编辑网卡配置文件 vi /etc/sysconfig/network-script/ifcfg-eth0 进入编辑模式 按i键进行编辑修改 DEVICE=eth0 #物理设备名 IPADDR=192. ...