Skip to content

Instantly share code, notes, and snippets.

@johnmccole
Last active April 11, 2023 22:36
Show Gist options
  • Save johnmccole/bcc721c23193161112dd412aaba22db0 to your computer and use it in GitHub Desktop.
Save johnmccole/bcc721c23193161112dd412aaba22db0 to your computer and use it in GitHub Desktop.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class Firing : MonoBehaviour
{
[Header("Weapons")]
[Header("Main Weapon")]
public GameObject mainWeapon;
public int minWeaponDamage;
public int maxWeaponDamage;
public float weaponFireRate;
[Header("Main Weapon")]
public GameObject backupWeapon;
[Header("Explosive Weapon")]
public GameObject explosiveWeapon;
public int maxGrenades = 1;
public int grenades = 1;
[Header("Special Weapon")]
public GameObject specialWeapon;
public bool canUseSpecial = false;
public float specialTimer;
private GameObject Player;
private PlayerControls playerControls;
private PlayerInput playerInput;
private bool isFiring;
public EnemyHealth enemyHealth { get; private set; }
PlayerLevel playerLevel;
void Awake()
{
Player = this.gameObject;
playerControls = new PlayerControls();
playerInput = Player.GetComponent<PlayerInput>();
playerLevel = Player.GetComponent<PlayerLevel>();
}
private void OnEnable()
{
playerControls.Enable();
playerControls.Player.FireMain.performed += FireMain;
playerControls.Player.FireMain.canceled += FireMain;
playerControls.Player.FireSecondary.performed += FireSecondary;
playerControls.Player.FireExplosive.performed += FireExplosive;
playerControls.Player.FireSpecial.performed += FireSpecial;
}
private void OnDisable()
{
playerControls.Disable();
playerControls.Player.FireMain.performed -= FireMain;
playerControls.Player.FireMain.canceled -= FireMain;
playerControls.Player.FireSecondary.performed -= FireSecondary;
playerControls.Player.FireExplosive.performed -= FireExplosive;
playerControls.Player.FireSpecial.performed -= FireSpecial;
}
public void FireMain(InputAction.CallbackContext obj)
{
// Button is held down
if ( obj.performed )
{
isFiring = true;
}
// Button is released
else
{
isFiring = false;
}
// Run the Coroutine
StartCoroutine( loopFireMain() );
}
// The Coroutine to run
IEnumerator loopFireMain()
{
// Check isFiring is true
if ( isFiring )
{
// Fire Raycast forward from the player
Ray mainRay = new Ray(Player.transform.position, Player.transform.forward);
// Check the Raycast hits anything
if ( Physics.Raycast(mainRay, out RaycastHit mainHit, 100f) )
{
// Check an object with the "Enemy" tag is hit
if (mainHit.collider.tag == "Enemy")
{
// Get the Player level, use this is a damage modifier
int levelModifier = playerLevel.playerLevel;
// Get the Damage range for the weapon
int weaponDamage = UnityEngine.Random.Range(minWeaponDamage, maxWeaponDamage);
// Modify damage based on Player level
weaponDamage = weaponDamage + 10 * levelModifier;
// Set up function to pass damage value to EnemyHealth script
void mainHitDamage(int weaponDamage)
{
enemyHealth = mainHit.collider.GetComponent<EnemyHealth>();
enemyHealth.enemyHealth -= weaponDamage;
enemyHealth.updateHealth();
}
// Perform the function
mainHitDamage(weaponDamage);
// Debug.Log($" Weapon Damage: {weaponDamage}");
}
}
// Wait the duration of the weapon fire rate if button is held
yield return new WaitForSeconds(weaponFireRate);
// Start the Coroutine
StartCoroutine( loopFireMain() );
}
}
private void FireSecondary(InputAction.CallbackContext obj)
{
Ray backupRay = new Ray(Player.transform.position, Player.transform.forward);
if (Physics.Raycast(backupRay, out RaycastHit backupHit, 100f))
{
// hit code here
if (backupHit.collider.tag == "Enemy")
{
Debug.Log("Fire Backup");
}
}
}
private void FireExplosive(InputAction.CallbackContext obj)
{
if ( grenades > 0 )
{
Debug.Log("Fire Explosive");
grenades--;
}
}
private void FireSpecial(InputAction.CallbackContext obj)
{
if ( canUseSpecial )
{
Debug.Log("Fire Special");
canUseSpecial = false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment