Lock Planar(鎖定地面)
阿新 • • 發佈:2018-12-13
解決模型跳起來之後亂動(例如跳起來之後可以轉方向)問題
將地面鎖死
public void OnjumpEnter() { pi.inputEnabled = false; //跳起來之後Dmag值就請零 //print("OnJump Enter!!!!!!!");//測試FSMOnEnter的訊號有沒有傳送到此層級 } public void OnjumpExit() { pi.inputEnabled = true; //落地可以移動 //print("OnJump Exit!!!!!!!");//測試FSMOnExit的訊號有沒有傳送到此層級 }
這樣跳起來後就不會亂動,但是會出現一個新的問題:跳起來之後不能向前移動
可以增加一個判斷(這裡我將MovingVec替換成了planarVec)
using System.Collections; using System.Collections.Generic; using UnityEngine; public class ActorController : MonoBehaviour { public GameObject model; public PlayerInput pi; public float WalkSpeed = 2.0f;//走路速度 視覺化控制 public float runMultiplier = 2.0f;//跑步速度 視覺化控制 [SerializeField]//把Animator暫時顯示到unity private Animator anim; private Rigidbody rigid;//Rigidbody 不能在updata中呼叫 要在Fixedupdata中呼叫 private Vector3 planarVec;//用於儲存玩家意圖 private bool lockPlanar = false;//是否鎖死地面移動 // Use this for initialization void Awake() { //獲取元件 pi = GetComponent<PlayerInput>();// 獲得玩家輸入鍵位 anim = model.GetComponent<Animator>();//吃一個模型 rigid = GetComponent<Rigidbody>();//移動位移 } // Update is called once per frame void Update() { //座標系計算+動畫補間旋轉 float targetRunMulti = ((pi.run) ? 2.0f : 1.0f);//走路到跑步切換加入緩動 anim.SetFloat("forward", pi.Dmag*Mathf.Lerp(anim.GetFloat("forward"),targetRunMulti,0.5f));//把Dup的值餵給Animator裡面的forwad //新增跳躍 if (pi.jump) { anim.SetTrigger("jump"); } if (pi.Dmag > 0.1f)//增加判斷 如果按鍵時間大於0.1秒那麼就不轉回到前面 { Vector3 targetForward = Vector3.Slerp(model.transform.forward, pi.Dvec, 0.3f);//轉身緩動 使模型在一個球面上旋轉 model.transform.forward = targetForward; } if(lockPlanar == false) { planarVec = pi.Dmag * model.transform.forward * WalkSpeed * ((pi.run) ? runMultiplier : 1.0f);//如果lockPlanaer等於false就更新planarvec的值,如果等於true那麼就鎖死在當前值 } //planarVec = pi.Dmag * model.transform.forward*WalkSpeed*((pi.run)?runMultiplier:1.0f);//玩家操作存量大小*當下模型正面=玩家意圖 把玩家意圖存在這個變數裡面 } private void FixedUpdate() //undata是每秒60幀,達到和顯示屏一樣的重新整理速度 而fixedupdata(物理引擎) 每秒50幀 { //rigid.position += planarVec * Time.fixedDeltaTime;//增加一段距離()每秒1個單位 速度乘時間來改位移 rigid.velocity = new Vector3(planarVec.x, rigid.velocity.y, planarVec.z);//直接指派速度 } public void OnjumpEnter() { pi.inputEnabled = false; //跳起來之後不能移動 //print("OnJump Enter!!!!!!!");//測試FSMOnEnter的訊號有沒有傳送到此層級 lockPlanar = true; } public void OnjumpExit() { pi.inputEnabled = true; //落地可以移動 //print("OnJump Exit!!!!!!!");//測試FSMOnExit的訊號有沒有傳送到此層級 lockPlanar = false; } }