41 lines
1.1 KiB
C#
41 lines
1.1 KiB
C#
|
|
using UnityEngine;
|
||
|
|
|
||
|
|
|
||
|
|
public class FPS_Controller : MonoBehaviour
|
||
|
|
{
|
||
|
|
[SerializeField] private CharacterController CharacterController;
|
||
|
|
[SerializeField] private float MoveSpeed;
|
||
|
|
[SerializeField] private float MouseSpeed;
|
||
|
|
[SerializeField] private Transform Camera;
|
||
|
|
Vector3 translationV3 = new Vector3();
|
||
|
|
|
||
|
|
private float mouseX;
|
||
|
|
private float mouseY;
|
||
|
|
private float XRotation;
|
||
|
|
|
||
|
|
private void Awake()
|
||
|
|
{
|
||
|
|
Cursor.lockState = CursorLockMode.Locked;
|
||
|
|
}
|
||
|
|
|
||
|
|
private void Update()
|
||
|
|
{
|
||
|
|
translationV3.x = Input.GetAxis("Vertical");
|
||
|
|
translationV3.z = Input.GetAxis("Horizontal");
|
||
|
|
|
||
|
|
mouseX = Input.GetAxis("Mouse X") * MouseSpeed * Time.deltaTime;
|
||
|
|
mouseY = Input.GetAxis("Mouse Y") * MouseSpeed * Time.deltaTime;
|
||
|
|
|
||
|
|
XRotation -= mouseY;
|
||
|
|
XRotation = Mathf.Clamp(XRotation, -90, 90);
|
||
|
|
|
||
|
|
Camera.localRotation = Quaternion.Euler(XRotation, 0f, 0f);
|
||
|
|
transform.Rotate(Vector3.up * mouseX);
|
||
|
|
|
||
|
|
translationV3 = transform.forward * translationV3.x + transform.right * translationV3.z;
|
||
|
|
translationV3.y = -50f * Time.deltaTime;
|
||
|
|
CharacterController.Move(translationV3 * MoveSpeed * Time.deltaTime);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|