0
\$\begingroup\$

So I have an empty object called player with the PlayerMovement script and a rigidbody. I have the main camera and player visual as childs of it. The movement works fine as long as I freeze the rigidbody's x and z rotation, but why is that? Here is my player code (minus a couple of things that would just make it harder for you to help me):

using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerMovement : MonoBehaviour { [SerializeField] private float speed = 5f; private float xLook; private float yLook; [SerializeField] private float _mouseSensitivity; [SerializeField] private float _upAndDownMax; [SerializeField] private Rigidbody rb; private Camera _camera; private Vector3 movement; void Start() { rb = GetComponent<Rigidbody>(); _camera = Camera.main; } void Update() { HandleMoveInput(); HandleLookInput(); } private void HandleMoveInput() { float hor = Input.GetAxisRaw("Horizontal"); float ver = Input.GetAxisRaw("Vertical"); Vector3 moveInput = new Vector3(hor, 0, ver); moveInput = transform.forward * moveInput.z + transform.right * moveInput.x; moveInput.Normalize(); movement = moveInput * speed; } private void HandleLookInput() { xLook += Input.GetAxis("Mouse X") * _mouseSensitivity * Time.deltaTime; // transform.rotation = Quaternion.Euler(0, xLook, 0); rb.MoveRotation(Quaternion.Euler(0, xLook, 0)); yLook -= Input.GetAxis("Mouse Y") * _mouseSensitivity * Time.deltaTime; yLook = Mathf.Clamp(yLook, -_upAndDownMax, _upAndDownMax); _camera.transform.localRotation = Quaternion.Euler(yLook, 0, 0); } void FixedUpdate() { MovePlayer(); // LookPlayer(); } private void MovePlayer() { Vector3 currentVelocity = rb.velocity; Vector3 targetVelocity = new Vector3(movement.x, currentVelocity.y, movement.z); Vector3 force = targetVelocity - currentVelocity; force.y = 0; rb.AddForce(force, ForceMode.VelocityChange); } } 

So I am guessing the problem is inside HandleMoveInput but I am not sure. I am just wondering why I have to freeze the rotation of z and x to make it work.

The second problem is that the camera sometimes start to stutter when colliding with things. I tested and I believe its because of the freeze problem and that its a child of the player. Because it does not happen when its not a child. Is it a bad idea to make the camera I child of the player?

If it's of any help: Editor screenshot

\$\endgroup\$
10
  • \$\begingroup\$ It looks like we're missing the LookPlayer method. Have you tried setting the angular velocity to zero? \$\endgroup\$ Commented Jun 10, 2024 at 16:23
  • \$\begingroup\$ Sorry the LookPlayer method was just this " rb.MoveRotation(Quaternion.Euler(0, xLook, 0));" which is under the "HandleLookInput" I had it in FixedUpdate once because its a rigidbody method, but that produced a camera with bad fps(but thats only because the method was called less, so I switched it back to Update) \$\endgroup\$ Commented Jun 10, 2024 at 17:40
  • \$\begingroup\$ When you mean "Angular Velocity" you are referring to "Angular drag", right? I have tried and I still got that camera jerk. It seems to happen whenever I collide with the side of an object while still going in that direction. You know when you go in the direction of a wall and your character gets "stuck", because your character is applying force in that direction and if you stop applying force your character falls down. But I think the reason is because the camera is a child and the collision or the input does something to the character which the camera dislike. \$\endgroup\$ Commented Jun 10, 2024 at 17:50
  • \$\begingroup\$ No, I use the words "angular velocity" to mean exactly that: angular velocity. \$\endgroup\$ Commented Jun 10, 2024 at 18:30
  • \$\begingroup\$ Well it seems like that worked? I dont get the camera stutter anymore and I dont have to freeze the rotation of certain axis. But that still brings me the question, why do I have to change the angular velocity/freeze rotation to make the character walk? Also Angular velocity, thats the current rotational velocity, right? I put this on update " rb.angularVelocity = new Vector3(0, 0, 0);" but shouldnt that prevent me from moving the y axis? But I can still do that? I guess thats because " rb.MoveRotation(Quaternion.Euler(0, xLook, 0));" overrides that? \$\endgroup\$ Commented Jun 10, 2024 at 19:10

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.