Cinemachine をカメラとして使用して 3 人称の動きのスクリプトを作成しようとしています。Brackeys の「THIRD PERSON MOVEMENT in Unity」YouTube チュートリアルに従いました。次に、そのベースをキャラクターコントローラーからリジッドボディに変更すると、動きは完全に正常に機能します。ただし、私のコードでは、重力と戦うプレーヤーを移動すると、リジッドボディの y 軸の速度が 0 に設定され、移動するとプレーヤーがゆっくりと地面にジッターします。ただし、プレイヤーが動きを止めると、キャラクターは地面に落ちます。必要なのは、スクリプトが y 軸を無視し、ユニティの重力を聞くことだけです。
void Update()
{
if (!photonView.isMine)
{
Destroy(GetComponentInChildren<Camera>().gameObject);
Destroy(GetComponentInChildren<Cinemachine.CinemachineFreeLook>().gameObject);
return;
}
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
isGrounded = Physics.CheckSphere(new Vector3(transform.position.x, transform.position.y - 1, transform.position.z), 0.01f, layerMask);
Vector3 inputVector = new Vector3(horizontal, 0f, vertical).normalized;
if (inputVector.magnitude >= 0.1f)
{
float targetAngle = Mathf.Atan2(inputVector.x, inputVector.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
rb.velocity = moveDir.normalized * speed;
}
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
rb.AddForce(Vector3.up * jumpForce);
}
}