3rd person character controller rotation keeps resetting to 0
My 3rd person character controller keeps forcing the players Y rotation transform to zero when I am not moving them/when there is no movement input. my console is also spammed with..
"Look rotation viewing vector is zero UnityEngine.Quaternion:LookRotation (UnityEngine.Vector3,UnityEngine.Vector3) PlayerController:Update () (at Assets/Scripts/PlayerController.cs:25 "
This is the script I am working with as well as the video for reference...
using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerController : MonoBehaviour
{
public float moveSpeed = 5.0f;
public float rotationSpeed = 280.0f;
public bool idleRotate = false;
float horizontal;
float vertical;
private void Update()
{
Vector3 moveDirection = Vector3.forward * vertical + Vector3.right * horizontal;
Vector3 projectedCameraForward = Vector3.ProjectOnPlane(Camera.main.transform.forward, Vector3.up);
Quaternion rotationToCamera = Quaternion.LookRotation(projectedCameraForward, Vector3.up);
//"shooter controller" transform.rotation = Quaternion.RotateTowards(transform.rotation, rotationToCamera, rotationSpeed * Time.deltaTime);
moveDirection = rotationToCamera * moveDirection;
Quaternion rotationToMoveDirection = Quaternion.LookRotation(moveDirection, Vector3.up);
//"shooter controller" transform.rotation = Quaternion.RotateTowards(transform.rotation, rotationToCamera, rotationSpeed * Time.deltaTime);
transform.rotation = Quaternion.RotateTowards(transform.rotation, rotationToMoveDirection, rotationSpeed * Time.deltaTime);
transform.position += moveDirection * moveSpeed * Time.deltaTime;
}
public void OnMoveInput(float horizontal, float vertical)
{
this.vertical = vertical;
this.horizontal = horizontal;
//Debug.Log($"Player Controller: Move Input: {vertical}, {horizontal} ");
}
}
link:Video tutorial referenced above
I have been trying to figure it out for a bit, but I am still learning the ropes for coding and could use the help. Much appreciated!
Your answer
Follow this Question
Related Questions
Object with fixed position but follows camera rotation 0 Answers
Mobile Gyroscope, make Camera always rotating towards zero point using Quaternion 2 Answers
Using a directional vector to orient a sphere? 0 Answers
Quaternion AngleAxis on pitch give random small value on others axis 0 Answers
Help with suns rotation 0 Answers