- Home /
Eliminate Roll effect in fps using keyboard
Hey all, I'm making a splitscreen game in first person, and the second player uses the keyboard to both move and look around. Problem is when turning without the camera being perfectly level horizontally, the whole camera becomes askew. I've seen come things about using Mathf.Clamp but all in regards to mouse controlled camera movement and I'm not sure how to apply it to keyboard use. I am using a character controller not a rigidbody so im not able to fix the rotation. Here is my player controller.
using UnityEngine; using System.Collections;
public class PlayerTwo : MonoBehaviour {
public float speed;
public float jumpSpeed;
public float gravity;
private Vector3 moveDirection = Vector3.zero;
// Update is called once per frame
void FixedUpdate()
{
Movement();
}
void Movement()
{
CharacterController controller = GetComponent<CharacterController>();
//applies gravity to character controller
if (controller.isGrounded)
{
moveDirection = new Vector3(Input.GetAxis("Horizontal2"), 0, Input.GetAxis("Vertical2"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton("Jump2"))
moveDirection.y = jumpSpeed;
//moves camera with
if (Input.GetKey(KeyCode.J))
{
transform.Rotate(0, -3, 0);
}
if (Input.GetKey(KeyCode.L))
{
transform.Rotate(0, 3, 0);
}
if (Input.GetKey(KeyCode.I))
{
transform.Rotate(-3, 0, 0);
}
if (Input.GetKey(KeyCode.K))
{
transform.Rotate(3, 0, 0);
}
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612093647im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Change the roll of the rotation 0 Answers
How can I rotate the player to fixed rotation? 1 Answer
First person controls and Portals 0 Answers
How to work out dogfight rotations for AI 0 Answers
Move object 1 Answer