Hi,
I’m trying to create a very basic fps game in unity. Unfortunately I have encountered a problem which is that my character’s camera can’t look up. Whenever I try to go up and down with my mouse, it feels like the vision is stuck. I have a video of what happens when I experimented to show the problem. Here is the link:
Im almost sure that my problem comes from a coding mistake. Can y’all help me?
my Input Manager class
public class InputManager : MonoBehaviour
{
private PlayerInput playerInput;
private PlayerInput.OnFootActions OnFoot;
private PlayerMotor motor;
private PlayerLook look;
// Start is called before the first frame update
void Awake()
{
playerInput = new PlayerInput();
OnFoot = playerInput.OnFoot;
motor = GetComponent<PlayerMotor>();
look = GetComponent<PlayerLook>();
// ctx = callback context to call the jump fct
OnFoot.Jump.performed += ctx => motor.Jump();
}
// Update is called once per frame
void FixedUpdate()
{
motor.ProcessMove(OnFoot.Movement.ReadValue<Vector2>());
}
void LateUpdate()
{
look.ProcessLook(OnFoot.Look.ReadValue<Vector2>());
}
private void OnEnable()
{
OnFoot.Enable();
}
private void OnDisable()
{
OnFoot.Disable();
}
}
My Player Look class
public class PlayerLook : MonoBehaviour
{
public Camera cam;
private float xRotation = 0f;
public float xSensitivity = 30f;
public float ySensitivity = 30f;
public void ProcessLook (Vector2 input)
{
float mouseX = input.x;
float mouseY = input.y;
xRotation = (mouseY * Time.deltaTime) * ySensitivity;
xRotation = Mathf.Clamp(xRotation, -120f, 120f);
cam.transform.localRotation = Quaternion.Euler(xRotation, 0, 0);
transform.Rotate(Vector3.up * (mouseX * Time.deltaTime) * xSensitivity);
}
}
My Player Motor class
public class PlayerMotor : MonoBehaviour
{
private CharacterController controller;
private Vector3 playerVelocity;
private float speed = 5f;
private bool isGrounded;
public float gravity = -9.8f;
public float jumpHeight = 5f;
// Start is called before the first frame update
void Start()
{
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
isGrounded = controller.isGrounded;
}
//receive input from input manager script and apply them to
// our character
public void ProcessMove(Vector2 input)
{
Vector3 moveDirection = Vector3.zero;
moveDirection.x = input.x;
moveDirection.z = input.y;
controller.Move(transform.TransformDirection(moveDirection) * speed * Time.deltaTime);
playerVelocity.y += gravity * Time.deltaTime;
controller.Move(playerVelocity * Time.deltaTime);
if(isGrounded && playerVelocity.y < 0)
{
playerVelocity.y = -3f;
}
}
public void Jump()
{
if(isGrounded)
{
playerVelocity.y = Mathf.Sqrt(jumpHeight * -4.0f * gravity);
}
}
}
