banner



How To Stop Camera From Rotating Unity

So far learning unity, camera rotation with mouse is the matter that has given me the near problems. At present the problem I am facing is, when making a first person controller based on rigidbody, that the rotation of the grapheme and camera with the mouse is inconsistent and jittery.

This youtube video, shows the problem. In it, i am smoothly moving the mouse but the rotation seems to sometimes skip some intermediate rotations and leap to new values.

This is the hierarchy I am currently using for the character as well as the fastened components: Image showing the hierarchy and attached components of a rigidbody based character

And this is the complete code for the script

                      using System.Collections;     using Organization.Collections.Generic;     using UnityEngine;      [RequireComponent(typeof(Rigidbody), typeof(Collider))]     public class CompleteRBController : MonoBehaviour     {     [Header("Referenced components:")]     [SerializeField]     individual Rigidbody _rb;     [SerializeField]     private Transform _cameraTransform;       [Header("Mouse Settings")]     public bladder _mouseSensitivity = 100f;     [SerializeField]     individual float pitch = 0f;     [SerializeField]     private bool _mouseInverted = false;       [Header("Parameters:")]     [SerializeField]     private float _speed = 10f;     [SerializeField]     individual float _sprintSpeed = 15f;     // Start is called before the first frame update     void Starting time()     {         Cursor.lockState = CursorLockMode.Locked;     }      // Update is called once per frame     void Update()     {      }      private void FixedUpdate()     {         //Become input mouse         float mouseX = Input.GetAxisRaw("Mouse 10") * _mouseSensitivity * Time.fixedDeltaTime;         float mouseY = Input.GetAxisRaw("Mouse Y") * _mouseSensitivity * Time.fixedDeltaTime;          //Get input keyboard         bladder vertical = Input.GetAxisRaw("Vertical");         bladder horizontal = Input.GetAxisRaw("Horizontal");          // Debug.Log(string.Format("Vertical: {0}, Horizontal: {1}", vertical, horizontal));          bool isSprinting = Input.GetKey(KeyCode.LeftShift);          //Rotate         pitch += (_mouseInverted) ? -mouseY : mouseY;         pitch = Mathf.Clench(pitch, -89, 89);          _cameraTransform.localRotation = Quaternion.Euler(pitch, 0, 0);         transform.Rotate(transform.up * mouseX);          //Motion         Vector3 movement = (transform.forwards * vertical + transform.right * horizontal).normalized * ((isSprinting)? _sprintSpeed : _speed);         _rb.velocity = new Vector3(motility.10, _rb.velocity.y, move.z);     }  }                  

I volition too add a list of things I tried to alter to make it smooth:

  • Changing getAxisRaw with getAxis. No effect on the smoothness of the camera movement.
  • Lerping the vertical rotation. Adds besides much filibuster, does not actually seem suitable for FPS controller? How would I lerp the yaw?
  • Move the rotation from FixedUpdate to Update. Does not do anything, and we are editing a rigidbody so information technology would be still better to do it on fixedUpdate with all other physic related calculations.
  • Brand carve up components one for the rotation and the other for the motion. does non help either.

Sometimes on the first seconds of the simulation i does piece of work smoothly but after a little while it becomes wonky again.

I dont really know how to fix it / what is causing the problem. I would really appreciate information technology if you could help me with this or put me in the right track to agreement the problem.

Thanks in advance for helping me.

Source: https://stackoverflow.com/questions/67222028/unity-inconsistent-camera-rotation-with-mouse

Posted by: nelsonbluddre.blogspot.com

0 Response to "How To Stop Camera From Rotating Unity"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel