PHASE THREE
- 设置Main Camera的Position为(1,15,-22),Rotation为(30,0,0),其Camera组件的Projection中选择Orthographic模式,Size值设为4.5,Background Color设为黑色;
- 为Main Camera创建脚本CameraFollow;
- CameraFollow中的Target变量指向Player;
- 保存Player作为Prefab。
脚本CameraFollow解析
using UnityEngine;
using System.Collections;
namespace CompleteProject
{
public class CameraFollow : MonoBehaviour
{
public Transform target;// 用target指定Player,作为摄像机跟随的目标
public float smoothing = 5f;// 设置摄像机跟随的快慢
Vector3 offset;// 定义一个向量offset
void Start ()
{
offset = transform.position - target.position;//保存Player到摄像机向量的初始值
}
void FixedUpdate ()
{
Vector3 targetCamPos = target.position + offset;//跟随摄像机应该在的位置为Player坐标加上初始的偏移向量
transform.position = Vector3.Lerp (transform.position, targetCamPos, smoothing * Time.deltaTime);//在固定时间内摄像机从当前位置移动到应该在的跟随位置
}
}
}
PHASE FOUR
- 将Zombunny模型置入Scene中,Prefabs文件夹中的HitParticles放进Zombunny中,选择Shootable层;
- 添加组件Rigidbody,将Drag及Angular Drag设为Infinity,约束锁定Position Y和Rotation X、Z;
- 添加组件CapsuleCollider,将Center Y设为0.8,Height设置1.5;
- 添加组件Sphere Collider,勾选Is Trigger box,将Center Y和Radius设置0.8;
- 添加Audio,选择Zombunny Hurt,不勾选Play On Awake;
- 添加组件Navigation→Nav Mesh Agent,设置Radius为0.3,Speed为3,Stopping Distance为1.3,Height为1.1;
- Window中打开Navigation窗口,选择Bake栏,设置Radius为0.75,Height为1.2,Step Height为0.1,Width Inaccuracy为1,点击Bake;
- 新建EnemyAC,将Zombunny中的动画拖入其中,Move设为默认,新建两个Tragger变量命名为PlayerDead、Dead,Move指向Idle,Condition设为PlayerDead,Any State指向Death,Condition设为Dead;
- 为Zombunny编写脚本EnemyMovement。
脚本EnemyMovement解析
using UnityEngine;
using System.Collections;
namespace CompleteProject
{
public class EnemyMovement : MonoBehaviour
{
Transform player; // 定义引用Player位置
PlayerHealth playerHealth; // 定义引用脚本PlayerHealth
EnemyHealth enemyHealth; // 定义引用脚本EnemyHealth
UnityEngine.AI.NavMeshAgent nav; // 定义引用NavMeshAgent
void Awake ()
{
player = GameObject.FindGameObjectWithTag ("Player").transform; //Player的位置坐标
playerHealth = player.GetComponent <PlayerHealth> (); //获取脚本组件PlayerHealth
enemyHealth = GetComponent <EnemyHealth> ();//获取脚本组件EnemyHealth
nav = GetComponent <UnityEngine.AI.NavMeshAgent> ();//获取寻路导航
}
void Update ()
{
if(enemyHealth.currentHealth > 0 && playerHealth.currentHealth > 0)//如果enemy和player的当前血量大于0
{
nav.SetDestination (player.position);//通过寻路导航设置目标点位玩家所在位置;
}
else
{
// ... disable the nav mesh agent.
nav.enabled = false; //如果enemy和player其中之一的血量小于等于0,则关闭寻路导航;
}
}
}
}