Skip to content

Instantly share code, notes, and snippets.

@SenpaiRar
Created May 6, 2020 07:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SenpaiRar/c95181d52506dcfd4be2ee70333eba53 to your computer and use it in GitHub Desktop.
Save SenpaiRar/c95181d52506dcfd4be2ee70333eba53 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerShoot : MonoBehaviour
{
public List<WeaponObject> ListOfWeapons;
public AudioSource Audiosrc;
public AudioClip SwitchSound;
public AudioClip NoSwitchSound;
public WeaponObject CurrentWeaponObject{
get{
return ListOfWeapons[CurrentWeapon];
}
}
public float WeaponSwitchCoolDown;
int CurrentWeapon;
float timeSinceLastShot;
float timeSinceLastSwitch;
private void Start(){
CurrentWeapon = 0;
timeSinceLastShot = 100;
timeSinceLastSwitch = 100;
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Mouse0) && timeSinceLastShot >= ListOfWeapons[CurrentWeapon].CooldownTime)
{
Shoot();
}
SwitchWeapon();
timeSinceLastShot += Time.deltaTime;
timeSinceLastSwitch +=Time.deltaTime;
}
void SwitchWeapon(){
if(Input.GetKeyUp(KeyCode.Q)){
if(timeSinceLastSwitch > WeaponSwitchCoolDown){
timeSinceLastSwitch = 0;
Audiosrc.PlayOneShot(SwitchSound, AudioConstant.AudioScale);
if(CurrentWeapon ==0){
CurrentWeapon = ListOfWeapons.Count-1;
}
else{
CurrentWeapon--;
}
}
else{
Audiosrc.PlayOneShot(NoSwitchSound, AudioConstant.AudioScale);
}
}
if(Input.GetKeyUp(KeyCode.E)){
if(timeSinceLastSwitch > WeaponSwitchCoolDown){
timeSinceLastSwitch = 0;
Audiosrc.PlayOneShot(SwitchSound, AudioConstant.AudioScale);
if(CurrentWeapon + 1 == ListOfWeapons.Count){
CurrentWeapon = 0;
}
else{
CurrentWeapon++;
}
}
else{
Audiosrc.PlayOneShot(NoSwitchSound, AudioConstant.AudioScale);
}
}
}
void Shoot()
{
Instantiate(ListOfWeapons[CurrentWeapon].BulletObject, transform.position, transform.rotation);
Audiosrc.PlayOneShot(ListOfWeapons[CurrentWeapon].ShootSound, AudioConstant.AudioScale);
timeSinceLastShot = 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment