Untitled
using UnityEngine; public class SimpleFPSController : MonoBehaviour { public float moveSpeed = 5f; // Movement speed of the player public float mouseSensitivity = 2f; // Sensitivity of the mouse movement private float verticalRotation = 0f; // Rotation around the X-axis private float verticalRotationLimit = 80f; // Limit to how much you can look up and down void Update() { // Handle mouse rotation float horizontalRotation = Input.GetAxis("Mouse X") * mouseSensitivity; transform.Rotate(0, horizontalRotation, 0); verticalRotation -= Input.GetAxis("Mouse Y") * mouseSensitivity; verticalRotation = Mathf.Clamp(verticalRotation, -verticalRotationLimit, verticalRotationLimit); Camera.main.transform.localRotation = Quaternion.Euler(verticalRotation, 0, 0); // Handle movement float forwardMovement = Input.GetAxis("Vertical") * moveSpeed; float strafeMovement = Input.GetAxis("Horizontal") * moveSpeed; Vector3 movement = new Vector3(strafeMovement, 0, forwardMovement); movement = transform.rotation * movement; transform.Translate(movement * Time.deltaTime, Space.World); } }
Leave a Comment