PHASE ONE
- 將Environment、Lights置入新建的場景中,設(shè)置Position(0,0,0);
- 生成GameObject → 3D Object → Quad;重命名為Floor,在Transform中設(shè)置Rotation(90,0,0),Scale(100,100,1);
- Floor中移除Mesh Renderer組件, layer選擇Floor層;
- GameObeject →Create Empty 重命名為BackgroundMusic,通過Add Component→ Audio → Audio Source , Audio Clip →Circle Select → Background Music 添加背景音樂,勾選Loop,Volume為0.1。
PHASE TWO
- 將Player置入場景中,設(shè)置Position(0,0,0),設(shè)置Tag為Player;
- 新建Animator Controller,命名為PlayerAC,將其置入場景中并雙擊打開,展開Player模型,將Idle,Move和Death動畫拖入PlayerAC中;
- 右鍵點擊Idle選擇Set as Default,新建一個bool變量命名為IsWalking,新建一個Trigger變量命名為Die;
- 右鍵點擊Idle選擇Make Transition指向Move,選擇生成的流程線設(shè)置Condition為IsWalking = true;
- 右鍵點擊Move選擇Make Transition指向Idle,選擇生成的流程線設(shè)置Condition為IsWalking = false;
- 右鍵點擊Any State選擇Make Transition指向Death,選擇生成的流程線設(shè)置Condition為Die(trigger);
- 為Player添加Rigidbody組件,將Drag和Angular Drag設(shè)置為Infinity,展開Constraints,鎖定Y Position及X、Z Rotations;
- 為Player添加組件Capsule Collider,設(shè)置Center(0.2,0.6,0),設(shè)置Height為1.2;
- 為Player添加音樂組件Player Hurt,不勾選Play On Awake;
- 為Player編寫Scripts,PlayerMovement。
腳本PlayerMovement解析:
using UnityEngine;
using UnitySampleAssets.CrossPlatformInput;
namespace CompleteProject
{
public float speed = 6f;// 定義角色的移動速度
Vector3 movement;// 角色的移動向量
Animator anim; // 角色動畫組件
Rigidbody playerRigidbody; //角色剛體組件
int floorMask; // 地板遮罩層用于攝像捕捉
float camRayLength = 100f; //從攝像機(jī)到屏幕里的射線長度
void Awake ()
{
floorMask = LayerMask.GetMask ("Floor");// 創(chuàng)建一個地板的遮罩層
anim = GetComponent <Animator> ();//引用Player的組件Animator
playerRigidbody = GetComponent <Rigidbody> ();//引用Player的組件Rigidbody
}
void FixedUpdate ()
{
float h =CrossPlatformInputManager.GetAxisRaw("Horizontal"); // 存儲鍵盤輸入的水平坐標(biāo)
float v = CrossPlatformInputManager.GetAxisRaw("Vertical");// 存儲鍵盤輸入的垂直坐標(biāo)
Move (h, v);//移動Player
Turning ();//使Player跟隨鼠標(biāo)指針轉(zhuǎn)向
Animating (h, v);//Player的動畫控制
}
void Move (float h, float v)
{
movement.Set (h, 0f, v);//根據(jù)鍵盤的輸入設(shè)置角色的移動向量
movement = movement.normalized * speed * Time.deltaTime;//對鍵盤的輸入向量進(jìn)行單位化,給它一個速度和幀時間來得到一幀移動的向量;
// Move the player to it's current position plus the movement.
playerRigidbody.MovePosition (transform.position + movement);//用Player的當(dāng)前坐標(biāo)加上movement的坐標(biāo)得到目標(biāo)位置坐標(biāo),移動Player到目標(biāo)位置
}
void Turning ()
{
Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);//獲取攝像機(jī)和鼠標(biāo)指針?biāo)邳c連成的線
RaycastHit floorHit;//射線命中點
// Perform the raycast and if it hits something on the floor layer...
if(Physics.Raycast (camRay, out floorHit, camRayLength, floorMask))//射線是否命中地板遮罩層
{
Vector3 playerToMouse = floorHit.point - transform.position;//獲取向量從角色位置指向射線命中地板的點
playerToMouse.y = 0f;//確保這個向量始終在地板平面上
// Create a quaternion (rotation) based on looking down the vector from the player to the mouse.
Quaternion newRotatation = Quaternion.LookRotation (playerToMouse);//創(chuàng)建一個注視旋轉(zhuǎn),從垂直上方看著地板上向量playerToMouse的沿著Y軸的旋轉(zhuǎn)軸角
playerRigidbody.MoveRotation (newRotatation);//使Player根據(jù)newRotatation這個軸角進(jìn)行旋轉(zhuǎn)
}
}
void Animating (float h, float v)
{
bool walking = h != 0f || v != 0f;//創(chuàng)建一個布爾值,當(dāng)鍵盤輸入不為0時,即角色移動時,布爾值為真,否則為假;
anim.SetBool ("IsWalking", walking);//對Player的Animator組件中的條件字段賦值,進(jìn)行真假判斷,來執(zhí)行角色移動動畫或者放置動畫;
}
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。