Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- [RequireComponent(typeof(CharacterController))]
- public class FirstPersonController : MonoBehaviour
- {
- [Header("Movement Settings")]
- public float moveSpeed = 5f;
- public float jumpHeight = 1.5f;
- public float gravity = -9.81f;
- [Header("Mouse Look Settings")]
- public float mouseSensitivity = 100f;
- public Transform playerCamera;
- private float xRotation = 0f;
- private CharacterController controller;
- private Vector3 velocity;
- private bool isGrounded;
- void Start()
- {
- controller = GetComponent<CharacterController>();
- Cursor.lockState = CursorLockMode.Locked; // Hides cursor
- }
- void Update()
- {
- HandleMovement();
- HandleMouseLook();
- }
- void HandleMovement()
- {
- isGrounded = controller.isGrounded;
- if (isGrounded && velocity.y < 0)
- {
- velocity.y = -2f; // Small downward force to stick to ground
- }
- float x = Input.GetAxis("Horizontal");
- float z = Input.GetAxis("Vertical");
- Vector3 move = transform.right * x + transform.forward * z;
- controller.Move(move * moveSpeed * Time.deltaTime);
- if (Input.GetButtonDown("Jump") && isGrounded)
- {
- velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
- }
- velocity.y += gravity * Time.deltaTime;
- controller.Move(velocity * Time.deltaTime);
- }
- void HandleMouseLook()
- {
- 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);
- playerCamera.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
- transform.Rotate(Vector3.up * mouseX);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment