Unity学习笔记七 - Survival Shooter Tutorial
来源:互联网 发布:网页三剑客过时了 知乎 编辑:程序博客网 时间:2024/05/18 02:44
下面来看下EnemyHealth.cs脚本中的内容
void Awake ()
{
anim = GetComponent <Animator> ();
enemyAudio = GetComponent <AudioSource> ();
hitParticles = GetComponentInChildren <ParticleSystem> ();
capsuleCollider = GetComponent <CapsuleCollider> ();
currentHealth = startingHealth;
}
void Update ()
{
if(isSinking) //变量isSinking为true
{
transform.Translate (-Vector3.up * sinkSpeed * Time.deltaTime); //使怪物的坐标逐渐下移,就是消失在地板下
}
}
public void TakeDamage (int amount, Vector3 hitPoint)
{
if(isDead) //如果isDead为true,就是已经置位,不执行后面的
return;
enemyAudio.Play (); //播放enemy上的AudioSource组件中的音乐
currentHealth -= amount;//当前血量减少amount
hitParticles.transform.position = hitPoint; //enemy上的hitParticles粒子效果位置设定在光束击中的点,根据前面的代码
hitParticles.Play(); //播放粒子效果
if(currentHealth <= 0) //当前血量小于等于0,执行Death函数
{
Death ();
}
}
void Death ()
{
isDead = true; //设置isDead = true,不用再死了
capsuleCollider.isTrigger = true; //本enemy上的capsuleCollider IsTrigger 设置为true (1)
anim.SetTrigger ("Dead"); //触发本enemy的animator的组件
enemyAudio.clip = deathClip; //设置本object的音乐片段为deathClip=死亡音乐片段
enemyAudio.Play (); //播放音乐
}
public void StartSinking ()
{
GetComponent <NavMeshAgent> ().enabled = false; //禁用NavMeshAgent组件,这是一个地图路径寻址导航的Navigation的执行单元,就是其可在Navigation制作和设定好的地图路线上寻路
GetComponent <Rigidbody> ().isKinematic = true; //设置刚体组件的isKinematic为true (2)
isSinking = true; //把isSinking设为true,enemy可以通过transform.Translate强行下移了
ScoreManager.score += scoreValue; //分数记录脚本ScoreManager的score,被声明为public static,分数记录脚本ScoreManager的score,被声明为public static,作为类的不分实体而存在的变量,有独立的存储空间,不论类被new了几次,它的存储空间都不变
Destroy (gameObject, 2f); //两秒钟后销毁此enemy
void Awake ()
{
anim = GetComponent <Animator> ();
enemyAudio = GetComponent <AudioSource> ();
hitParticles = GetComponentInChildren <ParticleSystem> ();
capsuleCollider = GetComponent <CapsuleCollider> ();
currentHealth = startingHealth;
}
void Update ()
{
if(isSinking) //变量isSinking为true
{
transform.Translate (-Vector3.up * sinkSpeed * Time.deltaTime); //使怪物的坐标逐渐下移,就是消失在地板下
}
}
public void TakeDamage (int amount, Vector3 hitPoint)
{
if(isDead) //如果isDead为true,就是已经置位,不执行后面的
return;
enemyAudio.Play (); //播放enemy上的AudioSource组件中的音乐
currentHealth -= amount;//当前血量减少amount
hitParticles.transform.position = hitPoint; //enemy上的hitParticles粒子效果位置设定在光束击中的点,根据前面的代码
hitParticles.Play(); //播放粒子效果
if(currentHealth <= 0) //当前血量小于等于0,执行Death函数
{
Death ();
}
}
void Death ()
{
isDead = true; //设置isDead = true,不用再死了
capsuleCollider.isTrigger = true; //本enemy上的capsuleCollider IsTrigger 设置为true (1)
anim.SetTrigger ("Dead"); //触发本enemy的animator的组件
enemyAudio.clip = deathClip; //设置本object的音乐片段为deathClip=死亡音乐片段
enemyAudio.Play (); //播放音乐
}
public void StartSinking ()
{
GetComponent <NavMeshAgent> ().enabled = false; //禁用NavMeshAgent组件,这是一个地图路径寻址导航的Navigation的执行单元,就是其可在Navigation制作和设定好的地图路线上寻路
GetComponent <Rigidbody> ().isKinematic = true; //设置刚体组件的isKinematic为true (2)
isSinking = true; //把isSinking设为true,enemy可以通过transform.Translate强行下移了
ScoreManager.score += scoreValue; //分数记录脚本ScoreManager的score,被声明为public static,分数记录脚本ScoreManager的score,被声明为public static,作为类的不分实体而存在的变量,有独立的存储空间,不论类被new了几次,它的存储空间都不变
Destroy (gameObject, 2f); //两秒钟后销毁此enemy
}
我在百度网盘上传的工程的一个bug,大象类怪物不会执行动画和下沉Animator设置有错
Prefabs->Hellephant中Animator组件的Controller选择为HellephantAOC,Animator override Controller,覆盖类,继承EnemyAnimatorController,原始是Hellephant的Move、Idle、Death覆盖的是zombunny的对应动画,而Animator中的转换关系和触发变量不变
附加TIPs:
1.我们如果注释掉(1),(2)两行,enemy还是继续下沉,说明 transform.Translate (-Vector3.up * sinkSpeed * Time.deltaTime);使武器强行移动
2.如果我们注释掉 transform.Translate (-Vector3.up * sinkSpeed * Time.deltaTime);,enemy不下沉,只是2秒后会消失
0 0
- Unity学习笔记七 - Survival Shooter Tutorial
- Unity学习笔记一 - Survival Shooter Tutorial
- Unity学习笔记二 - Survival Shooter Tutorial
- Unity学习笔记三 - Survival Shooter Tutorial
- Unity学习笔记四 - Survival Shooter Tutorial
- Unity学习笔记五 - Survival Shooter Tutorial
- Unity学习笔记六 - Survival Shooter Tutorial
- Unity学习笔记八 - Survival Shooter Tutorial
- Unity学习笔记九 - Survival Shooter Tutorial
- Unity学习笔记(四)----Survival Shooter
- Unity学习笔记(五)----Survival Shooter
- 学习Unity官方教程-Survival Shooter tutorialの声明
- Unity实例.003官方示例Survival Shooter Tutorial核心代码学习
- unity官方demo学习:Survival Shooter
- unity survival shooter ZSpace
- [初学Untiy]Survival Shooter Tutorial
- Unity2017官方案例Survival Shooter tutorial 浅析学习
- SURVIVAL SHOOTER TUTORIAL之Environment setup [1]
- Java中equals和==的区别
- 浙江省赛zoj3946 Highway Project
- 目录
- MySQL存储引擎--细说MyISAM与InnoDB区别
- C++第4次作业
- Unity学习笔记七 - Survival Shooter Tutorial
- Android 系统相册不可见问题
- windows 卷管理API
- java学习之二:构造方法
- leetcode20-valid parenthese
- Unity AssetBundle打包与资源更新
- C++类实现公有继承
- UVA11548 DP计算添加多少元素可以构成回文字符串
- Java技术核心卷I学习笔记——基本的程序设计结构