Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using UnityEngine;
- using UnityEngine.InputSystem;
- namespace GameFlight
- {
- public class Inputs : MonoBehaviour
- {
- public PlayerInput _playerInputs;
- public bool LeftMousePressed => Mouse.current.leftButton.wasPressedThisFrame;
- public bool RightMousePressed => Mouse.current.rightButton.wasPressedThisFrame;
- void OnEnable() => _playerInputs.onActionTriggered += OnActionTriggered;
- void OnActionTriggered(InputAction.CallbackContext ctx)
- {
- }
- void OnDisable() => _playerInputs.onActionTriggered -= OnActionTriggered;
- public bool Pressed(string action) => _playerInputs.actions[action].WasPerformedThisFrame();
- public bool Held(string action) => _playerInputs.actions[action].IsPressed();
- public bool Released(string action) => _playerInputs.actions[action].WasReleasedThisFrame();
- public float Value(string action) => _playerInputs.actions[action].ReadValue<float>();
- public Vector2 Axis2D(string action) => _playerInputs.actions[action].ReadValue<Vector2>();
- }
- }
- using System;
- using UnityEngine;
- namespace GameFlight
- {
- public class Player : MonoBehaviour
- {
- public CharacterCamera CharacterCamera;
- public Transform CameraFollowPoint;
- public Character Character;
- Inputs _inputs;
- void Awake()
- {
- _inputs = App.Get<Inputs>();
- Cursor.lockState = CursorLockMode.Locked;
- }
- private void Start()
- {
- CharacterCamera.SetFollowTransform(CameraFollowPoint);
- CharacterCamera.IgnoredColliders.AddRange(Character.GetComponentsInChildren<Collider>());
- }
- private void Update()
- {
- if (_inputs.LeftMousePressed) Cursor.lockState = CursorLockMode.Locked;
- HandleCharacterInput();
- }
- private void HandleCharacterInput()
- {
- if (!Character) return;
- Vector3 move = _inputs.Axis2D(InputActionNames.Move);
- CharInputs characterInputs = new CharInputs
- {
- MoveAxisForward = move.y,
- MoveAxisRight = move.x,
- CameraRotation = CharacterCamera.Transform.rotation,
- JumpDown = _inputs.Pressed(InputActionNames.Jump),
- CrouchDown = _inputs.Pressed(InputActionNames.Crouch),
- CrouchUp = _inputs.Released(InputActionNames.Crouch),
- Interact = _inputs.Pressed(InputActionNames.Interact),
- };
- Character.SetInputs(ref characterInputs);
- }
- }
- }
- using UnityEngine;
- namespace GameFlight
- {
- public static class InputActionNames
- {
- public static readonly string CameraMovement = "CameraMovement";
- public static readonly string Look = "Look";
- public static readonly string Move = "Move";
- public static readonly string Zoom = "Zoom";
- public static readonly string Jump = "Jump";
- public static readonly string Crouch = "Crouch";
- public static readonly string Interact = "Interact";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment