Hey there, I currently have a few weapon categories for my game world and all semi auto weapons are currently working with my U# scripts. The expected behavior for each is a bullet is fired when a player uses the item.
Where I am having trouble is automatic weapons where I want to recursively call the Shoot(); function until the player releases the use button. I’ve checked the U# API and the closest things I could find that would achieve this is calling InputUse() inside the Shoot() method or to recursively call either Shoot() or OnPickupUseDown(). I’ve attached the code block I am trying to edit for automatic fire. Would anyone be willing to help push me in the right direction?
//Function to fire weapon
public override void OnPickupUseDown()
{
if(AmmoCount > 0 && arAnimator.GetCurrentAnimatorStateInfo(0).IsName("ReadyToFire")) //Check to see if player has ammo and if the AR is not in a shot delay animation
{
Debug.Log("Player fired weapon.");
Shoot();
} else
{
Debug.Log("Player is out of ammo or gun is not ready to fire.");
}
}
//Method used to facilitate bullet logic
private void Shoot()
{
//Play gunshot sound
GunShot.Play();
//Play shot delay animation
arAnimator.Play("ShotDelay");
//Subtract 1 from AmmoCount
AmmoCount -= 1;
//Define direction for Ray
Vector3 direction = Vector3.left;
//Define RaycastHit for finding data on what the Ray hit | Used in "out" statement of Physics.Raycast method
//Cast out Ray and output GameObject that the Ray hit
//Physics.Raycast(barrel.position, barrel.TransformDirection(direction * Range), out HitData, Range) | This line of code returns true or false if the Ray hits something
if (Physics.Raycast(barrel.position, barrel.TransformDirection(direction * Range), out RaycastHit HitData, Range)) //Check to see if Ray hit any colliders
{
//Empty if statement for now until EnemyScript is implemented
}
}```