using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem; namespace BulletHellTemplate { /// /// Handles player movement using configurable keys for each direction and mouse aiming for PC with the new Input System. /// public class PCInputController : MonoBehaviour { private CharacterEntity characterEntity; private Camera mainCamera; [Header("Directional Keys")] [Tooltip("Keys for moving upwards")] public List upKeys = new List { Key.W, Key.UpArrow }; [Tooltip("Keys for moving downwards")] public List downKeys = new List { Key.S, Key.DownArrow }; [Tooltip("Keys for moving left")] public List leftKeys = new List { Key.A, Key.LeftArrow }; [Tooltip("Keys for moving right")] public List rightKeys = new List { Key.D, Key.RightArrow }; private void Start() { #if UNITY_6000_0_OR_NEWER characterEntity = FindFirstObjectByType(); #else CharacterEntity characterEntity = FindObjectOfType(); #endif mainCamera = Camera.main; } private void Update() { if (characterEntity != null) { // Get movement direction from configured keys Vector3 direction = GetMovementInput(); characterEntity.Move(direction); // Update aim direction based on mouse position UpdateAimDirection(); } } /// /// Gets the movement input based on the configured keys. /// /// A Vector3 representing the movement direction. private Vector3 GetMovementInput() { float moveX = 0f; float moveZ = 0f; if (IsAnyKeyPressed(upKeys)) moveZ += 1f; if (IsAnyKeyPressed(downKeys)) moveZ -= 1f; if (IsAnyKeyPressed(rightKeys)) moveX += 1f; if (IsAnyKeyPressed(leftKeys)) moveX -= 1f; return new Vector3(moveX, 0, moveZ).normalized; } /// /// Checks if any key in a list of keys is pressed. /// private bool IsAnyKeyPressed(List keys) { foreach (var key in keys) { if (Keyboard.current[key].isPressed) return true; } return false; } /// /// Updates the character's aim direction based on mouse position. /// private void UpdateAimDirection() { Vector2 aimDirection = GetMouseAimDirection(); // characterEntity.UpdateDirectionalAim(aimDirection); } /// /// Calculates the aim direction from the character to the mouse position. /// /// Normalized direction vector. private Vector2 GetMouseAimDirection() { Ray ray = mainCamera.ScreenPointToRay(Mouse.current.position.ReadValue()); Plane plane = new Plane(Vector3.up, characterEntity.transform.position); if (plane.Raycast(ray, out float distance)) { Vector3 point = ray.GetPoint(distance); Vector3 direction = point - characterEntity.transform.position; return new Vector2(direction.x, direction.z).normalized; } return Vector2.zero; } } }