我不太確定如何表達這個問題,對此感到抱歉。我在為角色設定影片以學習自上而下的角色控制器(無剛體)時遇到了這個障礙我在十年前的 Reddit 和統一論壇上看到過這個問題,但我不太明白他們的答案.
我現在的問題是我無法找到一種方法來告訴 mecanim 玩家是否正朝著他們所面對的方向移動。例如,如果玩家向左移動并瞄準左側,則應該播放 moveForward 影片。如果玩家向左移動但瞄準向右,則應該播放 moveBackwards 影片。
這是我第一次發布問題。如果我的格式錯誤,我很抱歉。這是我的代碼,我將不勝感激。
public class CharacterMovement : MonoBehaviour
{
private Vector3 velocity;
private Vector3 PlayerMoveInput;
Animator animationS;
[SerializeField] CharacterController characterrController;
[SerializeField] private float MoveSpeed;
[SerializeField] private float JumpHeight;
[SerializeField] private float Gravity = -9.81f;
// Start is called before the first frame update
void Start()
{
animationS = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
PlayerMoveInput = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical")); //collects the input for the player
MovePlayer();
PlayerRotation();
}
private void MovePlayer()
{
if (characterrController.isGrounded)
{
velocity.y = -1f;
}
if (Input.GetKeyDown(KeyCode.Space) && characterrController.isGrounded)
{
velocity.y = JumpHeight;
}
///
else
{
velocity.y -= Gravity * -2f * Time.deltaTime;
}
Vector3 MoveVector = transform.TransformDirection(PlayerMoveInput);
characterrController.Move(MoveSpeed * Time.deltaTime * MoveVector);
characterrController.Move(velocity * Time.deltaTime);
float velocityX = Vector3.Dot(PlayerMoveInput, transform.forward);
float velocityZ = Vector3.Dot(PlayerMoveInput, transform.right);
animationS.SetFloat("velocityX", velocityZ, 0.1f, Time.deltaTime);
animationS.SetFloat("velocityZ", velocityX, 0.1f, Time.deltaTime);
}
void PlayerRotation()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
Debug.DrawRay(ray.origin, ray.direction, Color.yellow);
if (Physics.Raycast(ray, out hit))
{
Vector3 targetPosition = new Vector3(hit.point.x, transform.position.y, hit.point.z);
Quaternion rotation = Quaternion.LookRotation(targetPosition - transform.position);
transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * 10.0f);
}
}
}
uj5u.com熱心網友回復:
這不是將與您的代碼一起使用的確切實作,但應該給您一個良好的開端。
// get the angle between where the player is pointing, and where he is moving
float angle = Vector3.Angle(MoveVector, targetPosition - transform.position);
if (angle < 90f){
//move forward animation
} else {
//move back animation
}
我沒有測驗過這段代碼,但想法是如果玩家面對的方向和玩家移動的方向之間的角度小于 90 度,那么他正在向前移動。此外,此處使用的變數:MoveVector 和 targetPosition 是私有的,因此您需要先解決該問題,然后才能實作此方法。
uj5u.com熱心網友回復:
使用 mechanim,在您的影片師中擁有 4 個單獨的浮點值引數是很常見的。兩個用于移動資訊,兩個用于查看方向資訊。這使您可以使用基于那些處理很多痛苦的函式的混合樹,您需要做的就是根據您在做什么,在每次更新/后期更新時使用正確的值更新影片器中的值。
這就是我的影片師引數以及運動混合樹的布局方式,我在單獨的圖層上有另一個,僅用于使用 Head left right 的頭部,并且 head updown 引數用于控制頭部。
這是設定影片組件值的代碼(或至少其中一些)
編輯:
抱歉,忘了提到我在 anim 變數上呼叫“LerpFloat”的函式是我自己為 animator 組件定義的擴展方法,它所做的只是獲取浮點值,對其進行處理,然后將浮點值設定回來。它只使用 Mathf.Lerp。
uj5u.com熱心網友回復:
謝謝兩位的回答,問題是影片剪輯的設定......
你在哪里打勾
根變換 旋轉
根變換位置 (Y)
根變換位置 (XZ)
我已經勾選了所有這些,只需要根變換旋轉。將來我需要對影片師和影片剪輯更加小心,以免頭疼。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/459078.html
下一篇:返回列表