- Home /
Problems with Player Movement and Camera Movement clashing, any solutions?
Hi,
I've already done work on Unity2D and have decided to pop into Unity3D in order to experiment with some things. I set up some simple things around the scene (playing area and a player) to play around with things. I followed Brackeys' tutorial on a simple player movement & first person camera controller, as shown here: https://www.youtube.com/watch?v=_QajrabyTJc
After a bit of tweaking, both the player and the camera move fine individually. The problem arises when they're both used at the same time; if I were to move any direction whilst moving the mouse, some 'jankiness' occurs.
Basically, the mouse begins to move in a jittery/choppy motion, and both mouse & movement inputs become quite delayed. This means that if I were to let go of my inputs it would take a while before the movements from the player or the camera actually stop. If I were to be going forward and then suddenly switch my input to go right (or look right) it would take a while for my player to actually turn right.
I hope my explanation makes sense. If not, ask me to clarify.
Here's my scripts (although I honestly doubt it's a problem in the scripts, please do prove me wrong if there is):
Player Movement
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
// Components
[SerializeField] private CharacterController charControl = null;
// Player Properties
[SerializeField] private float walkSpeed = 8f;
void Start() => charControl = GetComponent<CharacterController>();
void Update()
{
float moveX = Input.GetAxisRaw("Horizontal");
float moveZ = Input.GetAxisRaw("Vertical");
Vector3 moveDir = ((transform.right * moveX) + (transform.forward * moveZ)).normalized;
if (moveDir.magnitude >= 0.01f)
{
charControl.Move(moveDir * walkSpeed);
}
}
}
Camera Controller
using UnityEngine;
public class MouseMovement : MonoBehaviour
{
// Components
[SerializeField] private Transform playerTransform = null;
// Camera Properties
[SerializeField] private float mouseSensitivity = 10f;
private float xRot = 0f;
void Start() => Cursor.lockState = CursorLockMode.Locked;
void Update()
{
float mouseX = Input.GetAxisRaw("Mouse X");
float mouseY = Input.GetAxisRaw("Mouse Y");
// Determines the angle to rotate the Camera on the X axis (up and down)
xRot -= mouseY * mouseSensitivity;
xRot = Mathf.Clamp(xRot, -90f, 90f);
// Calculates how much the Player Transform needs to rotate
Vector3 yRot = Vector3.up * mouseX * mouseSensitivity;
transform.localRotation = Quaternion.Euler(xRot, 0f, 0f);
playerTransform.Rotate(yRot);
}
}
I've tweaked them, so they're a tiny bit different from the video but they're practically similar. I'll add this just in case: I've tried these out both with and without the tweaks, and they both showed the same problem, so the tweaks didn't affect anything.
I never had this issue with latency in Unity2D, so I honestly don't know what's going on. If I were to guess, I'd say it has to do with my framerate, but I slightly doubt that as well. My laptop is quite old, just for reference.
Ways I've tried to deal with it so far:
Printing the values of the necessary variables to the console (no surprise, the values behaved relative to what was going on on-screen).
Playing around with the mouse sensitivity. For some reason a higher sensitivity just slightly improves it, but we're talking about extremely high sensitivity values for very minor improvements.
Playing on Maximised mode (no difference).
Swapping the CharacterController for a Rigidbody-based player controller. I think I should test this once again, but a similar problem still persisted from it.
Changing the
Input.GetAxis()
parts (from Brackeys' video) toInput.GetAxisRaw()
.
Thanks for taking the time to read!
Your answer
Follow this Question
Related Questions
Problem in rotating camera around a moving player 1 Answer
Input System Can't Catch Event on Update 0 Answers
Why am I losing some inputs on Update function? 0 Answers
How do I have multiple controls for the same function? (Controller and Keyboard) 1 Answer
I'm a bit new but when my camera rotates with the player model to look around it kind of jitters 0 Answers