- Home /
FPS Controller rotation,FPS Controller rotation problem
Sorry for bad English :) I'm from Slovenia.
I create my first 3d game and I followed Bracky tutorial ( https://www.youtube.com/watch?v=_QajrabyTJc ) Something works like looking up and down but if I want to move the mouse to left it goes for 1 sec but than glitched back to the center. Please help :) Thanks here is my code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class MouseLook : MonoBehaviour {
public float mouseSensitivity = 100f; public Transform playerBody; float xRotation = 0f; // Start is called before the first frame update void Start() { Cursor.lockState = CursorLockMode.Locked; } // Update is called once per frame 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); playerBody.Rotate(Vector3.up * mouseX); } }
, 1. Sorry for bad English, I'm from Slovenia :). 2. So I want to make 3d game and I follow the Bracky tutorial. ( https://www.youtube.com/watch?v=_QajrabyTJc ) I type everything just like he and it's not working well. My rotation in Y is working (up and down) but in X (left and right) not. If I drag the mouse in left or right it goes for 1 sec and glitched back in center.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseLook : MonoBehaviour
{
public float mouseSensitivity = 100f;
public Transform playerBody;
float xRotation = 0f;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
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);
playerBody.Rotate(Vector3.up * mouseX);
}
}