Hello guys!
I’m facing a wird problem. My player only turn making a circle and I would like to turn the player in place, withoud the player run in circles. I will need to use a rigidbody for this project and I would like to move the player using rigidbody, but when I try it the camera start to jitter, this is another problem that i’m facing… I’m using a cinemachine camera and the new input system.
i’m trying to make something like in this video:
Thanks for all.
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;
}
}