本章節(jié)我們就來(lái)使用水平軸和垂直軸來(lái)控制游戲物體的移動(dòng)和旋轉(zhuǎn)。我們之前大致講過(guò),游戲物體移動(dòng)最重要的是方向,速度和時(shí)間三個(gè)要素,同樣旋轉(zhuǎn)也是。接下來(lái),我們將之前創(chuàng)建的地形場(chǎng)景導(dǎo)入進(jìn)來(lái),如下所示
然后將之前的“MecanimDemo”工程里面的模型文件和動(dòng)畫文件拿過(guò)來(lái)使用,這里可以導(dǎo)入我自定義的“U_Character_Animatiion.unitypackage”資源包文件。
?
然后,我們將“U_Character_REF.fbx”模型拖入到場(chǎng)景中,
?
然后選中相機(jī),點(diǎn)擊菜單欄“GameObject”->“Align With View”調(diào)整Main Camera位置
接下來(lái), 我們創(chuàng)建一個(gè)“U_Character_Controller”動(dòng)畫控制器文件,雙擊打開它
然后將我們之前復(fù)制過(guò)來(lái)的兩個(gè)動(dòng)畫添加進(jìn)來(lái)
我們默認(rèn)動(dòng)畫是Idle休閑,然后在Idle和Run之間彼此建立過(guò)渡。接下來(lái),我們創(chuàng)建一個(gè)動(dòng)畫參數(shù)is_run,類型為bool類型,如下所示
接下來(lái),我們?cè)O(shè)置Idle到Run的過(guò)渡條件為is_run = true
然后Run到Idle的過(guò)渡條件為is_run = false
設(shè)置完畢后,我們將這個(gè)“U_Character_Controller.controller”文件拖拽到Animator組件的Controller項(xiàng)上面去。
最后,我們創(chuàng)建一個(gè)“MoveScript.cs”腳本文件來(lái)控制動(dòng)畫的播放,如下所示
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveScript : MonoBehaviour
{
// 動(dòng)畫播放組件
private Animator animator;
// Start is called before the first frame update
void Start()
{
// 獲取動(dòng)畫播放組件
animator = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
// 按下按鍵W播放動(dòng)畫
if (Input.GetKeyDown(KeyCode.W))
{
animator.SetBool("is_run", true);
}
// 抬起按鍵W停止播放動(dòng)畫
if (Input.GetKeyUp(KeyCode.W))
{
animator.SetBool("is_run", false);
}
}
}
然后,我們將這個(gè)腳本附加到“U_Character_REF”游戲?qū)ο笊厦?/span>
我們Play運(yùn)行工程查看一下。
現(xiàn)在我們的動(dòng)畫播放沒有問(wèn)題了。接下來(lái),我們就開始控制角色的移動(dòng)。我們之前講過(guò),這個(gè)最好使用“Character Controller”角色控制器組件來(lái)實(shí)現(xiàn)。
?
接下來(lái),我們就來(lái)修改“MoveScript.cs”腳本文件,增加移動(dòng)代碼,如下所示
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveScript : MonoBehaviour
{
// 動(dòng)畫播放組件
private Animator animator;
// 角色控制器組件
private CharacterController controller;
// 定義移動(dòng)速度和旋轉(zhuǎn)速度
private float moveSpeed = 10.0f;
private float rotateSpeed = 2.0f;
// Start is called before the first frame update
void Start()
{
// 獲取動(dòng)畫播放組件
animator = GetComponent<Animator>();
// 獲取角色控制器組件
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
// 垂直方向的輸入(WS按鍵)控制移動(dòng)
float v = Input.GetAxis("Vertical");
controller.SimpleMove(transform.forward * moveSpeed * v);
// 水平方向的輸入(AD按鍵)控制旋轉(zhuǎn)
float h = Input.GetAxis("Horizontal");
transform.Rotate(0, rotateSpeed * h, 0);
// 按下按鍵W播放動(dòng)畫
if (Input.GetKeyDown(KeyCode.W))
{
animator.SetBool("is_run", true);
}
// 抬起按鍵W停止播放動(dòng)畫
if (Input.GetKeyUp(KeyCode.W))
{
animator.SetBool("is_run", false);
}
}
}
接下來(lái),我們就來(lái)運(yùn)行工程。
接下來(lái),我們來(lái)添加相機(jī)跟隨角色移動(dòng),鼠標(biāo)控制相機(jī)旋轉(zhuǎn)的代碼。以前我們是將相機(jī)放置到角色的下面(成為角色的子游戲?qū)ο螅?,這種方式雖然簡(jiǎn)單,但是存在一些問(wèn)題。這里,我們將使用代碼來(lái)控制相機(jī)跟隨角色。原理很簡(jiǎn)單,就是當(dāng)角色移動(dòng)的時(shí)候,相機(jī)也跟隨移動(dòng)。我們創(chuàng)建一個(gè)新的腳本“CameraScript.cs”文件,內(nèi)容如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraScript : MonoBehaviour
{
// 游戲角色
private Transform player;
// 相機(jī)控制參數(shù)
private bool isRotating = false;
private Vector3 offset;
private float distance;
private float rotateSpeed = 2.0f;
// Start is called before the first frame update
void Start()
{
// 獲取游戲角色
player = GameObject.Find("U_Character_REF").transform;
// 相機(jī)和游戲角色的偏移
offset = transform.position - player.position;
// 相機(jī)和游戲角色之間的距離
distance = offset.magnitude;
}
// Update is called once per frame
void Update()
{
// 鼠標(biāo)右鍵控制相機(jī)旋轉(zhuǎn)
if(Input.GetMouseButtonDown(1)) isRotating = true;
if(Input.GetMouseButtonUp(1)) isRotating = false;
// 相機(jī)圍繞游戲角色旋轉(zhuǎn)
if (isRotating)
{
// 圍繞游戲角色的Y軸左右旋轉(zhuǎn)
transform.RotateAround(player.position, player.up, rotateSpeed * Input.GetAxis("Mouse X"));
// 圍繞相機(jī)(也可以試試游戲角色)的X軸上下旋轉(zhuǎn)
transform.RotateAround(player.position, transform.right, -rotateSpeed * Input.GetAxis("Mouse Y"));
}
// 根據(jù)滾輪來(lái)增加和減小這個(gè)距離
distance += Input.GetAxis("Mouse ScrollWheel") * 1.0f;
offset = offset.normalized * distance;
// 游戲角色移動(dòng)后,相機(jī)跟隨移動(dòng),保持偏移
transform.position = player.position + offset;
}
}
接下來(lái),我們要將這個(gè)腳本附加到相機(jī)上面。
接下來(lái),我們稍微調(diào)整一下視角,
接下來(lái),我們Play運(yùn)行整個(gè)工程
總結(jié):Unity推薦使用Input Manager來(lái)獲取設(shè)備的輸入信息。對(duì)于方向性輸入肯定是使用Input.GetAxis("Horizontal")和Input.GetAxis("Vertical")方法來(lái)獲取X/Y數(shù)值。這些X/Y數(shù)值可以直接用來(lái)控制游戲角色的移動(dòng)(后續(xù)章節(jié)我們會(huì)詳細(xì)介紹)。而對(duì)于按鈕的輸入,可以通過(guò)使用Input.GetButtonUp("Fire1")來(lái)獲取是否按下狀態(tài)。至于輸入軸對(duì)應(yīng)的什么類型的設(shè)備的按鍵,就可以在Input Manager來(lái)設(shè)置。可以是鍵盤,鼠標(biāo),或者游戲手柄。當(dāng)然,Unity默認(rèn)已經(jīng)給我們添加了18個(gè)輸入軸的設(shè)置。我們也可以繼續(xù)添加新的設(shè)備按鍵設(shè)置輸入軸。但是,這些設(shè)置對(duì)于我們的代碼修改則是沒有太大的影響。因?yàn)槲覀兇a中,不管使用GetAxis方法,還是GetButtonUp方法,它的參數(shù)僅僅是輸入軸的名稱而已。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-438035.html
本課程涉及的內(nèi)容已經(jīng)共享到百度網(wǎng)盤:https://pan.baidu.com/s/1e1jClK3MnN66GlxBmqoJWA?pwd=b2id文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-438035.html
到了這里,關(guān)于第五十一章 Unity Input Manager 輸入系統(tǒng)(下)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!