using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerController : MonoBehaviour { public float speed = 5f; public float mouseSensitivity = 2f; public Camera playerCamera; public GameObject bulletImpact; public GameObject playerModel; public float jumpForce = 8f; public float gravity = 9.8f; private float verticalRotation = 0f; private CharacterController controller; private Vector3 velocity; void Start() { controller = GetComponent(); Cursor.lockState = CursorLockMode.Locked; } void Update() { MovePlayer(); LookAround(); if (Input.GetButtonDown("Fire1")) { Shoot(); } if (Input.GetButtonDown("Jump") && controller.isGrounded) { velocity.y = jumpForce; } ApplyGravity(); } void MovePlayer() { float moveX = Input.GetAxis("Horizontal") * speed; float moveZ = Input.GetAxis("Vertical") * speed; Vector3 move = transform.right * moveX + transform.forward * moveZ; controller.Move(move * Time.deltaTime); } void LookAround() { float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity; float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity; verticalRotation -= mouseY; verticalRotation = Mathf.Clamp(verticalRotation, -90f, 90f); playerCamera.transform.localRotation = Quaternion.Euler(verticalRotation, 0f, 0f); transform.Rotate(Vector3.up * mouseX); } void Shoot() { RaycastHit hit; if (Physics.Raycast(playerCamera.transform.position, playerCamera.transform.forward, out hit, 100f)) { Debug.Log("Hit: " + hit.transform.name); if (hit.transform.CompareTag("Enemy")) { Destroy(hit.transform.gameObject); } Instantiate(bulletImpact, hit.point, Quaternion.LookRotation(hit.normal)); } } void ApplyGravity() { if (!controller.isGrounded) { velocity.y -= gravity * Time.deltaTime; } else if (velocity.y < 0) { velocity.y = -2f; } controller.Move(velocity * Time.deltaTime); } }