Untitled
unknown
plain_text
2 years ago
1.0 kB
2
Indexable
// Attach this script to your player character in Unity using UnityEngine; public class SimpleFPSController : MonoBehaviour { public float moveSpeed = 5f; public float mouseSensitivity = 2f; private Camera playerCamera; void Start() { Cursor.lockState = CursorLockMode.Locked; playerCamera = GetComponentInChildren<Camera>(); } void Update() { // Player movement float horizontal = Input.GetAxis("Horizontal"); float vertical = Input.GetAxis("Vertical"); Vector3 moveDirection = new Vector3(horizontal, 0f, vertical).normalized; Vector3 moveAmount = moveDirection * moveSpeed * Time.deltaTime; transform.Translate(moveAmount); // Player rotation based on mouse input float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity; float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity; transform.Rotate(Vector3.up * mouseX); playerCamera.transform.Rotate(Vector3.left * mouseY); } }
Editor is loading...
Leave a Comment