I have no clue if this is even the correct place to post, unity forums absolutely confuses the G out of me… Aaaaanyway!
I’m working on a game where the player will be mainly focusing on camera controlling, basically an upside down RPG of sorts if you will. It mostly works however, for one reason or another when I control my camera to be rotating and moving, it starts flicking aggressively down so it look directly at a 90 Degree angle on the ground, and I can’t for the life of me figure out what it may be. The related codes look like this.
void Update() {
if (isRotating) {
HandleMovement();
}
HandleRotation();
HandleZoom();
HandleMouseClick();
}
private void HandleMovement() {
if (isRotating) {
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
Vector3 right = mainCam.transform.right;
Vector3 forward = mainCam.transform.forward;
Vector3 up = mainCam.transform.up;
right.y = 0;
forward.y = 0;
right.Normalize();
forward.Normalize();
up.Normalize();
float speed = moveSpeed;
if (Input.GetKey(KeyCode.LeftShift)) {
speed = moveSpeed * 4.5f;
}
Vector3 moveDirection = (forward * vertical + right * horizontal) * speed * Time.deltaTime;
if (Input.GetKey(KeyCode.Space)) {
moveDirection += up * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.LeftControl)) {
moveDirection -= up * speed * Time.deltaTime;
}
transform.Translate(moveDirection, Space.World);
}
}
private void HandleRotation() {
if (Input.GetMouseButton(2)) {
float horizontal = Input.GetAxis("Mouse X") * lookSpeed;
float vertical = Input.GetAxis("Mouse Y") * lookSpeed;
Vector3 eulerRotation = transform.eulerAngles;
float newYaw = eulerRotation.y + horizontal;
float newPitch = Mathf.Clamp(eulerRotation.x - vertical, -89f, 89f);
transform.eulerAngles = new Vector3(newPitch, newYaw, 0f);
Cursor.lockState = CursorLockMode.Locked;
isRotating = true;
} else {
Cursor.lockState = CursorLockMode.None;
isRotating = false;
}
}