- Home /
Vertical mouse input inverted when player is turned around
Okay, sorry to bother everyone with such a simple issue, but I can't find a solution for it, and I figured I should get some help before I break my back on it.
Basically, I'm making a first person game, making my own movement scripts in the process, and to handle looking around, I'm rotating the player around the current position, triggered by mouse movements. The issue is, when the player turns around, the vertical mouse movements become inverted (moving the mouse up causes the player to look down and vice versa) which makes sense, but of course can't be stood for.
My current code goes as follows:
var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;
var rotSpeed : float = 5.0;
var latTarget : Transform;
var latRotSpeed : float = 5.0;
var target : Transform;
private var moveDirection : Vector3 = Vector3.zero;
function Update() {
var controller : CharacterController = GetComponent.<CharacterController>();
if (controller.isGrounded) {
// We are grounded, so recalculate
// move direction directly from axes
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
transform.RotateAround(target.position, Vector3.up, Input.GetAxis("Mouse X")*rotSpeed);
transform.RotateAround(latTarget.position, Vector3.left, Input.GetAxis("Mouse Y")*latRotSpeed);
//if (transform.eulerAngles.
transform.eulerAngles.z = 0;
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
controller.Move(moveDirection * Time.deltaTime);
}
The only significant thing I've done, outside of research, is try and move the vertical rotation on to a script for the camera, instead of the player, and having the vertical rotation revolve around an entity separate from the player, which had no effect. Please be aware I'm open to any solution, no matter how obscure, complicated, or jury-rigged it may seem.
EDIT: A lot of you are probably thinking i should just add a simple if-else statement, so that whenever they turn past the point of inversion, it inverts the controls again. the issue is, there is no real point of inversion, instead it just gradually gets harder and harder to look up or down, until it gets inverted.