unity官方demo學習之Stealth(十一)角色移動
阿新 • • 發佈:2019-02-11
十一,角色移動
引數: 喊叫音訊剪輯
玩家轉動速率
速度緩衝時間
動作Animator控制引用
HashID指令碼引用
函式:
awake: 獲得並設定動作控制器和HashID元件引用給變數
設定動畫層權重,第一個引數1代表層的序號shouting層,第二個引數代表權重,1代表完全覆蓋其它層
FixedUpdate(玩家是一個物理物件):獲取使用者輸入(橫,縱,潛行按鍵(根據座標軸名稱返回虛擬座標系的值(鍵盤為-1到1)))並儲存為變數(方便以後使用且不用再呼叫input函式)
呼叫MovementManagement(h, v, sneak)使玩家移動
Update:獲取輸入的喊叫資訊(按下那一幀返回true )並設定為bool值
將上面的bool值賦給動作控制器的shoutingBool變數從而播放相應動畫
呼叫AudioManagement 函式播放喊叫音效
MovementManagement(h, v, sneak):
為char_ethan新增指令碼DonePlayerMovement
using UnityEngine; using System.Collections; public class DonePlayerMovement : MonoBehaviour { public AudioClip shoutingClip; // Audio clip of the player shouting. public float turnSmoothing = 15f; // A smoothing value for turning the player. public float speedDampTime = 0.1f; // The damping for the speed parameter private Animator anim; // Reference to the animator component. private DoneHashIDs hash; // Reference to the HashIDs. void Awake () { // Setting up the references. anim = GetComponent<Animator>(); hash = GameObject.FindGameObjectWithTag(DoneTags.gameController).GetComponent<DoneHashIDs>(); // Set the weight of the shouting layer to 1. anim.SetLayerWeight(1, 1f); } void FixedUpdate () { // Cache the inputs. float h = Input.GetAxis("Horizontal"); float v = Input.GetAxis("Vertical"); bool sneak = Input.GetButton("Sneak"); MovementManagement(h, v, sneak); } void Update () { // Cache the attention attracting input. bool shout = Input.GetButtonDown("Attract"); // Set the animator shouting parameter. anim.SetBool(hash.shoutingBool, shout); AudioManagement(shout); } void MovementManagement (float horizontal, float vertical, bool sneaking) { // Set the sneaking parameter to the sneak input. anim.SetBool(hash.sneakingBool, sneaking); // If there is some axis input... if(horizontal != 0f || vertical != 0f) { // ... set the players rotation and set the speed parameter to 5.5f. Rotating(horizontal, vertical); anim.SetFloat(hash.speedFloat, 5.5f, speedDampTime, Time.deltaTime); } else // Otherwise set the speed parameter to 0. anim.SetFloat(hash.speedFloat, 0); } void Rotating (float horizontal, float vertical) { // Create a new vector of the horizontal and vertical inputs. Vector3 targetDirection = new Vector3(horizontal, 0f, vertical); // Create a rotation based on this new vector assuming that up is the global y axis. Quaternion targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up); // Create a rotation that is an increment closer to the target rotation from the player's rotation. Quaternion newRotation = Quaternion.Lerp(rigidbody.rotation, targetRotation, turnSmoothing * Time.deltaTime); // Change the players rotation to this new rotation. rigidbody.MoveRotation(newRotation); } void AudioManagement (bool shout) { // If the player is currently in the run state... if(anim.GetCurrentAnimatorStateInfo(0).nameHash == hash.locomotionState) { // ... and if the footsteps are not playing... if(!audio.isPlaying) // ... play them. audio.Play(); } else // Otherwise stop the footsteps. audio.Stop(); // If the shout input has been pressed... if(shout) // ... play the shouting clip where we are. AudioSource.PlayClipAtPoint(shoutingClip, transform.position); } }
引數: 喊叫音訊剪輯
玩家轉動速率
速度緩衝時間
動作Animator控制引用
HashID指令碼引用
函式:
awake: 獲得並設定動作控制器和HashID元件引用給變數
設定動畫層權重,第一個引數1代表層的序號shouting層,第二個引數代表權重,1代表完全覆蓋其它層
FixedUpdate(玩家是一個物理物件):獲取使用者輸入(橫,縱,潛行按鍵(根據座標軸名稱返回虛擬座標系的值(鍵盤為-1到1)))並儲存為變數(方便以後使用且不用再呼叫input函式)
呼叫MovementManagement(h, v, sneak)使玩家移動
Update:獲取輸入的喊叫資訊(按下那一幀返回true
將上面的bool值賦給動作控制器的shoutingBool變數從而播放相應動畫
呼叫AudioManagement 函式播放喊叫音效
MovementManagement(h, v, sneak):
設定引數sneakingBool的值
判斷如果有按橫向或縱向鍵的話,執行Rotating (float horizontal, float vertical)實現玩家轉動功能
設定玩家速度為5.5,速度緩衝時間
否則,設定速度為0
Rotating (float horizontal, float vertical):通過橫縱變數來控制玩家轉向
設定玩家要轉的目標方向,型別為vector3
將上述vector3變數設定為Quaternion(四元數,1,i,j,k),以便玩家轉向(LookRotation靜態函式可以使z軸轉向targetDirection(要面朝的方向),使y軸朝向上)
使玩家平滑的由當前角度轉向目標角度,Lerp(角色剛體當前旋轉值,目標旋轉值,旋轉速度)
設定玩家當前角度為轉過的新角度
AudioManagement (bool shout):
判斷如果動作控制器獲得的狀態資訊為locomotionState時
判斷沒播放時
播放腳步聲
若不在locomotionState狀態
停止播放腳步聲
若喊叫為真
使用AudioSource函式自動建立一個物件,並在指定地點播放音效,之後物件自動銷燬,不會一直存在於場景中
回到unity,將Audio中的player_attractAttention拖到剛剛指令碼中的Shouting Clip中,設定喊叫音效
最後注意勾選Animator下的apply root motin,使玩家可以移動
執行。。。玩一下