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,則關閉尋路導航;
}
}
}
}
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。