Mouvement issue with mouse input.
Okay so for being transparent i follow the brackeys tutorial for fps camera (I'm not creating a fps but the camera is the same). At the beginning it works fine. But after more script added to my project something strange happened. When i start the game, if i dont touch to my keyboard, the mouse(camera) movement works fine. But if a touch for exemple to the "W" key for go forward if i move my mouse my player go realy fast forward or backward (move on left and right). For sum up my issue, if a move my mouse, my player go outside of map, his position make no sens, and if i don't touch the mouse, the movement works fine. (Sorry for my english).
Script : Player controller
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
[Header("Move Settings")]
public float MoveSpeed = 5f;
public float JumpHeight = 20f;
[Space]
public LayerMask GroundLayerMask;
public Transform GroundCheck;
//
private bool isGrounded;
private CharacterController controller;
private Vector3 velocity;
private float gravity = -20f;
//public Camera Cam;
int nbJump = 0;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
controller = GetComponent<CharacterController>();
Cursor.visible = false;
}
void Update()
{
if (Input.GetKey(KeyCode.LeftShift))
run();
else
walk();
jump();
}
#region jump
void jump()
{
isGrounded = Physics.CheckSphere(GroundCheck.position, 1f, GroundLayerMask);
if (isGrounded)
nbJump = 0;
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
velocity.y = Mathf.Sqrt(JumpHeight * gravity * -2);
nbJump = 1;
}
if (nbJump < 2)
{
if (Input.GetKeyDown(KeyCode.Space))
{
velocity.y = Mathf.Sqrt(JumpHeight * gravity * -2);
nbJump = 2;
}
}
}
#endregion
#region walk & run
private void walk()
{
float x = Input.GetAxis("Horizontal");
float y = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * y;
controller.Move(move * MoveSpeed * Time.deltaTime);
controller.Move(velocity * Time.deltaTime);
velocity.y += gravity * Time.deltaTime;
}
private void run()
{
float x = Input.GetAxis("Horizontal");
float y = Input.GetAxis("Vertical");
//Cam.fieldOfView = Mathf.Lerp(120f, 100f, Time.deltaTime);
Vector3 move = transform.right * x + transform.forward * y;
controller.Move(move * (MoveSpeed * 2f) * Time.deltaTime);
controller.Move(velocity * Time.deltaTime);
velocity.y += gravity * Time.deltaTime;
}
#endregion
}
CameraScript :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerLook : MonoBehaviour
{
[Header("Player Settings")]
public float mouseSensitivity = 90.0f;
public Transform PlayerObject;
float xRotation = 0f;
void Start ()
{
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
}
void Update ()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
PlayerObject.Rotate(Vector3.up * mouseX);
}
}
Your answer
Follow this Question
Related Questions
How to have your player controls change while your camera rotates? 0 Answers
how to make camera follow player after a delay 1 Answer
If player hits certain X value in opposition to Camera, shift camera question. 0 Answers
Locking Camera's rotation on a rolling character 0 Answers
Trying to get camera to follow mouse, and player movement to be relative to camera angle? 0 Answers