I am currently using “Invoke Unity Event” to get an input and I am having trouble turning that into a Fire-Type action. With my current code, I get both the press and hold, and I can’t figure out how to change that to just a press.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class Shooting : MonoBehaviour
{
private bool shootbool;
private float intshoot;
public Transform firepoint;
public GameObject bulletPrefab;
public float bulletForce = 200f;
public void shoot(InputAction.CallbackContext shootValue){
shootbool = shootValue.ReadValueAsButton();
}
private void shooted()
{
GameObject Projectile = Instantiate(bulletPrefab, firepoint.position, firepoint.rotation);
Rigidbody2D rbp = Projectile.GetComponent<Rigidbody2D>();
rbp.AddForce(firepoint.right * bulletForce, ForceMode2D.Impulse);
}
private void FixedUpdate()
{
if(shootbool == true)
{
shooted();
}
}
}
It is currently acting like a hold where the bool stays true as long as I hold the bottom down, but I want it to only activate on the press of the button
you don’t need press and hold interactions just remove both, it’s not necessary
public void Shoot(InputAction.CallbackContext shootValue)
{
switch(shootValue.phase)
{
case InputActionPhase.Started:
{
//Start Phase common use for reset properties or active something
shootbool = true;
Debug.Log("I'm start shooting");
}
break;
case InputActionPhase.Performed:
{
//shootbool will stay true till Canceled is call
//this will deactivated shootbool while holding the button
//shootbool = false;
string text = (shootbool) ? "I'm still shooting" : "I'm done shooting";
Debug.Log(text);
}
break;
case InputActionPhase.Canceled:
{
string text = (shootbool) ? "I'm done shooting" : "I'm done shooting before you release the button";
//Reset properties or deactivated
shootbool = false;
Debug.Log(text);
}
break;
}
}