Hi, so I’ve been working on a basic first-person player controller with networking, but I’ve encountered a strange issue which I can’t seem to wrap my head around. Basically, using WASD and the mouse independently works just fine, but if I’m holding one of the WASD keys while moving the mouse at the same time, the player completely stops moving until the mouse stops moving.
My player is a simple capsule right now, and X-axis movements on the mouse rotate the capsule, while Y-axis movements on the mouse rotate another invisible GameObject inside of the capsule to look up and down.
Here are the relevant parts of my program:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
[RequireComponent(typeof(Rigidbody))]
public class Player : NetworkBehaviour
{
// Fields
[SerializeField]
private float movementSpeed = 5f;
[SerializeField]
private float jumpForce = 5f;
[SerializeField]
private float mouseSensitivity = 1f;
[SerializeField]
private Camera cameraComponent;
private Rigidbody rigidbodyComponent;
// Properties
public float MovementSpeed { get { return this.movementSpeed; } set { this.movementSpeed = value; } }
public float JumpForce { get { return this.jumpForce; } set { this.jumpForce = value; } }
public Rigidbody RigidbodyComponent { get { return this.rigidbodyComponent; } }
// Methods
private void Start()
{
this.rigidbodyComponent = this.GetComponent<Rigidbody>();
this.SetupCamera();
}
private void Update()
{
// Only accept input if this is the local player
if (!this.isLocalPlayer)
return;
// Keyboard/mouse controls
this.RotateCamera();
this.MovePlayer();
}
private void MovePlayer()
{
float inputX = Input.GetAxis("Horizontal"); // Left/Right
float inputZ = Input.GetAxis("Vertical"); // Forward/Back
float x = inputX * this.movementSpeed * Time.deltaTime;
float z = inputZ * this.movementSpeed * Time.deltaTime;
Vector3 desiredPosition = this.transform.position + (this.transform.right * x + this.transform.forward * z);
this.rigidbodyComponent.MovePosition(desiredPosition);
}
private void RotateCamera()
{
float inputX = Input.GetAxis("Mouse X");
float inputY = Input.GetAxis("Mouse Y");
float x = inputX * 1.5f * this.mouseSensitivity;
float y = inputY * 1.5f * this.mouseSensitivity;
Vector3 desiredAngle = new Vector3(this.cameraComponent.transform.rotation.eulerAngles.x, this.transform.rotation.eulerAngles.y, 0f);
desiredAngle.x -= y;
// Angle clamping
if (desiredAngle.x > 90f && desiredAngle.x < 180f) // Make sure the player can't vertically rotate their camera past 90 degreees (down)
{
desiredAngle.x = 90f;
}
else if (desiredAngle.x < 270f && desiredAngle.x > 180f) // Make sure the player can't vertically rotate their camera past 90 degreees (up)
{
desiredAngle.x = 270f;
}
this.transform.Rotate(0f, x, 0f);
this.cameraComponent.transform.rotation = Quaternion.Euler(desiredAngle);
}
private void SetupCamera()
{
// Only disable the camera if this is not the local player
if (this.isLocalPlayer)
return;
this.cameraComponent.gameObject.SetActive(false);
}
}
I’d greatly appreciate some help if anyone else can figure out what’s wrong.
Thanks in advance.