Hi! I’ll need to use a Rigidbory to my player in my game project, but when I try to move the player, my camera starts to jittering and the player dont’ fall. I’m using cinemachine.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed = 5.0f;
public float rotationSpeed = 280.0f;
float horizontal;
float vertical;
Rigidbody playerRigidbody;
private void Awake()
{
playerRigidbody = GetComponent<Rigidbody>();
}
private void Update()
{
Vector3 moveDirection = Vector3.forward * vertical + Vector3.right * horizontal;
Vector3 projectedCameraFoward = Vector3.ProjectOnPlane(Camera.main.transform.forward, Vector3.up);
Quaternion rotationCamera = Quaternion.LookRotation(projectedCameraFoward, Vector3.up);
moveDirection = rotationCamera * moveDirection;
Quaternion rotationToMoveDirection = Quaternion.LookRotation(moveDirection);
//transform.rotation = Quaternion.RotateTowards(transform.rotation, rotationCamera, rotationSpeed * Time.deltaTime);
if(moveDirection.magnitude >= 0.1f)
{
transform.rotation = Quaternion.RotateTowards(transform.rotation, rotationToMoveDirection, rotationSpeed * Time.deltaTime);
}
//transform.rotation = Quaternion.RotateTowards(transform.rotation, rotationToMoveDirection, rotationSpeed * Time.deltaTime);
//transform.position += moveDirection * moveSpeed * Time.deltaTime;
playerRigidbody.velocity = moveDirection * moveSpeed;
}
public void OnMoveInput(float horizontal, float vertical)
{
this.vertical = vertical;
this.horizontal = horizontal;
}
}