I copy this input manager script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Interactions;
namespace Game.Manager
{
public class InputManager : MonoBehaviour
{
[SerializeField] PlayerInput playerInput;
public Vector2 Move { get; private set;}
public Vector2 Look { get; private set;}
public bool Run { get; private set;}
public bool Jump { get; private set;}
public bool Fire { get; private set;}
InputActionMap currentMap;
InputAction moveAction;
InputAction lookAction;
InputAction runAction;
InputAction jumpAction;
InputAction fireAction;
private void Awake()
{
currentMap = playerInput.currentActionMap;
moveAction = currentMap.FindAction("Move");
lookAction = currentMap.FindAction("Look");
runAction = currentMap.FindAction("Run");
jumpAction = currentMap.FindAction("Jump");
fireAction = currentMap.FindAction("Fire");
moveAction.performed += onMove;
lookAction.performed += onLook;
runAction.performed += onRun;
jumpAction.performed += onJump;
fireAction.performed += onFire;
moveAction.canceled += onMove;
lookAction.canceled += onLook;
runAction.canceled += onRun;
jumpAction.canceled += onJump;
fireAction.canceled += onFire;
}
private void onMove(InputAction.CallbackContext context)
{
Move = context.ReadValue<Vector2>();
}
private void onLook(InputAction.CallbackContext context)
{
Look = context.ReadValue<Vector2>();
}
private void onRun(InputAction.CallbackContext context)
{
Run = context.ReadValueAsButton();
}
private void onJump(InputAction.CallbackContext context)
{
Jump = context.ReadValueAsButton();
}
private void onFire(InputAction.CallbackContext context)
{
Fire = context.ReadValueAsButton();
}
private void OnEnable()
{
currentMap.Enable();
}
private void OnDisable()
{
currentMap.Disable();
}
}
}
but i can’t detect if fire button is pressed.
This is the Gun script where i want to use fire button:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Game.Manager;
public class GunScript : MonoBehaviour
{
InputManager inputManager;
[SerializeField] Camera playerCamera;
[SerializeField] float maxDistance;
// Start is called before the first frame update
void Start()
{
inputManager = GetComponent<InputManager>();
}
// Update is called once per frame
void Update()
{
RaycastHit hit;
if (inputManager.Fire)
{
if (Physics.Raycast(playerCamera.transform.position, playerCamera.transform.forward, out hit, maxDistance))
{
Debug.Log("Shoot");
if (hit.transform.CompareTag("Enemy"))
{
Debug.Log("Enemy hit");
}
}
}
}
}
Console and VS don’t give me any errors but the first if statement doesn’t work at all.
I checked if there was something wrong on Input Action Asset but it is all correct.