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動(dòng)畫拖入PlayerAC中侈询;
- 右鍵點(diǎn)擊Idle選擇Set as Default,新建一個(gè)bool變量命名為IsWalking糯耍,新建一個(gè)Trigger變量命名為Die扔字;
- 右鍵點(diǎn)擊Idle選擇Make Transition指向Move,選擇生成的流程線設(shè)置Condition為IsWalking = true温技;
- 右鍵點(diǎn)擊Move選擇Make Transition指向Idle革为,選擇生成的流程線設(shè)置Condition為IsWalking = false;
- 右鍵點(diǎn)擊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;// 定義角色的移動(dòng)速度
Vector3 movement;// 角色的移動(dòng)向量
Animator anim; // 角色動(dòng)畫組件
Rigidbody playerRigidbody; //角色剛體組件
int floorMask; // 地板遮罩層用于攝像捕捉
float camRayLength = 100f; //從攝像機(jī)到屏幕里的射線長度
void Awake ()
{
floorMask = LayerMask.GetMask ("Floor");// 創(chuàng)建一個(gè)地板的遮罩層
anim = GetComponent <Animator> ();//引用Player的組件Animator
playerRigidbody = GetComponent <Rigidbody> ();//引用Player的組件Rigidbody
}
void FixedUpdate ()
{
float h =CrossPlatformInputManager.GetAxisRaw("Horizontal"); // 存儲(chǔ)鍵盤輸入的水平坐標(biāo)
float v = CrossPlatformInputManager.GetAxisRaw("Vertical");// 存儲(chǔ)鍵盤輸入的垂直坐標(biāo)
Move (h, v);//移動(dòng)Player
Turning ();//使Player跟隨鼠標(biāo)指針轉(zhuǎn)向
Animating (h, v);//Player的動(dòng)畫控制
}
void Move (float h, float v)
{
movement.Set (h, 0f, v);//根據(jù)鍵盤的輸入設(shè)置角色的移動(dòng)向量
movement = movement.normalized * speed * Time.deltaTime;//對(duì)鍵盤的輸入向量進(jìn)行單位化,給它一個(gè)速度和幀時(shí)間來得到一幀移動(dòng)的向量崎逃;
// 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)掷倔,移動(dòng)Player到目標(biāo)位置
}
void Turning ()
{
Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);//獲取攝像機(jī)和鼠標(biāo)指針?biāo)邳c(diǎn)連成的線
RaycastHit floorHit;//射線命中點(diǎn)
// 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;//獲取向量從角色位置指向射線命中地板的點(diǎn)
playerToMouse.y = 0f;//確保這個(gè)向量始終在地板平面上
// Create a quaternion (rotation) based on looking down the vector from the player to the mouse.
Quaternion newRotatation = Quaternion.LookRotation (playerToMouse);//創(chuàng)建一個(gè)注視旋轉(zhuǎn),從垂直上方看著地板上向量playerToMouse的沿著Y軸的旋轉(zhuǎn)軸角
playerRigidbody.MoveRotation (newRotatation);//使Player根據(jù)newRotatation這個(gè)軸角進(jìn)行旋轉(zhuǎn)
}
}
void Animating (float h, float v)
{
bool walking = h != 0f || v != 0f;//創(chuàng)建一個(gè)布爾值个绍,當(dāng)鍵盤輸入不為0時(shí)勒葱,即角色移動(dòng)時(shí),布爾值為真巴柿,否則為假凛虽;
anim.SetBool ("IsWalking", walking);//對(duì)Player的Animator組件中的條件字段賦值,進(jìn)行真假判斷广恢,來執(zhí)行角色移動(dòng)動(dòng)畫或者放置動(dòng)畫凯旋;
}
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者