第一種方法:使用Input.GetAxisRaw()方法
Input.GetAxisRaw是在UnityEngine里的內置方法,其用法為
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour
{
void Update()
{
float speed = Input.GetAxisRaw("Horizontal") * Time.deltaTime;
transform.Rotate(0, speed, 0);
}
}
如上代碼中的speed,這個變數會獲取到Input.GetAxisRaw的值(1||0||-1),我們可以用speed這個變數作為控制角色影片的判斷條件
如
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour
{
public Animator anim;//使用影片組件
void Update()
{
float speed = Input.GetAxisRaw("Horizontal") * Time.deltaTime;
transform.Rotate(0, speed, 0);
}
void SwitchAni()
{
if(speed == 1)
{
anim.SetBool("move",true);//move需要在unity編輯器中設定一個名為move的Parameters,設定好狀態機
}
}
}
那么當我們按右方向鍵的時候,speed的值變為1,那么播放人物朝向右行走的影片,簡單的人物移動就完成啦
第二種方法:使用Input.GetKey()方法
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour
{
public float speed;
void Update()
{
if (Input.GetKey(KeyCode.A))
{
transform.position += new Vector3(0.1f, 0, 0);
}
else if (Input.GetKey(KeyCode.D))
{
transform.position += new Vector3(-0.1f, 0, 0);
}
else if (Input.GetKey(KeyCode.W))
{
transform.position += new Vector3(0, 0, 0.1f);
}
else if (Input.GetKey(KeyCode.S))
{
transform.position += new Vector3(0, 0, -0.1f);
}
}
}
鍵盤監聽,輸入對應的鍵盤上的值便使其Input.GetKey()的值變為1,通過if判斷陳述句使人物進行移動
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/518866.html
標籤:其他
下一篇:[征途外掛制作記四]
