![](/img/trans.png)
[英]My Unity 2D player controller script won't make my character flip when he moves to a different direction
[英]My player character won't stop snapping to the original Y axis using Unity's character controller
我對 Unity 的角色控制器組件有一個非常令人沮喪的問題。 每當我移動我的播放器時,我的角色在移動時都會移動到它原來的 Y 軸值。 在我停止按下移動鍵之前,角色不會從那個 Y 位置落下。 我暫時使用了 Brackey 的教程,我什至不確定它是否是代碼本身,因為我刪除了它並使用了不同的系統並得到了相同的結果。 無論哪種方式,這是代碼:
private void FixedUpdate()
{
//Movement control. Explanation found here: https://docs.unity3d.com/ScriptReference/Input.GetAxis.html
//Grabs player input
float axisH = Input.GetAxisRaw("Horizontal");
float axisV = Input.GetAxisRaw("Vertical");
//Direction of player (linked to camera)
Vector3 direction = new Vector3(axisH, 0f, axisV).normalized;
float turnSmoothTime = 0.1f;
float turnSmoothVelocity = 0;
if (direction.magnitude >= 0.1f)
{
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
playerTransform.rotation = Quaternion.Euler(0f, angle, 0f);
// playerTransform.position += new Vector3(axisH, 0, axisV).normalized * speed; //Moves player based on movement
Vector3 moveDirection = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
characterController.Move(moveDirection.normalized * speed);
}
}
至於我的角色控制器組件的值,見下文:
我試圖將所有這些值更改為各種配置,但沒有成功。 我在這個論壇上找不到任何帖子或其他解決我的問題的帖子,那些提出類似問題的人在其他地方找到了解決方案。
任何幫助將不勝感激!
編輯:我意識到角色並沒有移動一個設定的數量,而是 Y 值在 Unity 中最初設置的值。
你能給一些關於這個問題的視頻嗎? 您也可以嘗試將剛體添加到角色控制器所在的游戲對象。 角色控制器的移動功能可能表現得很奇怪,我建議嘗試移動(您自己的值)來實際了解角色控制器如何移動其對象。 還嘗試使用 bruteforce 更改 Y 軸,在代碼中添加一些調試以了解 Y 值在何處以及如何更改。
https://docs.unity3d.com/ScriptReference/CharacterController.Move.html
也簡單的移動可能會更好;
https://docs.unity3d.com/ScriptReference/CharacterController.SimpleMove.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.