Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class PlayerGuns : MonoBehaviour
- {
- //current bugs, when you click q when number of guns equals either 1 or 3 its still destroys guns
- //list of numbers of guns
- public int[] guns;
- public AudioClip ColtShoot;
- public AudioClip ColtShootSecond;
- public int ChooseColt = 0;
- private AudioSource source;
- public float VolumeLowRange;
- public float VolumeHighRange;
- //gameobject guns, to be added
- GameObject[] gunsobj;
- //checks what 1 2 3 was pressed
- public float numberofguns;
- public float primaryammo;
- public float secondaryammo;
- //delay between shots for colt
- public float coltbetweenshots;
- //how long it takes to reload with colt
- public float reloadcolttime;
- //how big a colt clip is
- public float coltclip;
- //the damage the colt inflicts to enemy
- public float coltdamage;
- //the colt has not fired at start
- private bool firedcolt = false;
- //range of colt
- public float rangeofcolt;
- //how much reloadcolt
- public float reloadcolt = 8;
- private bool dontreloadeveryupdatecolt = false;
- public bool reloadedcolt = false;
- //time it takes to swing hammer
- public float hammerbetweenshots;
- //the damage the hammer deals
- public float hammerdamage;
- //the hammer has not fired at start
- public bool firedhammer = false;
- //check if the player collided with colt
- public bool collidedwithcolt;
- public bool collidedwithhammer;
- //check if the colt was already created
- public bool createdguncolt;
- public bool createdhammer;
- //just a double check for the colt
- public bool doublecheckcolt;
- //fake colt gameobject
- private GameObject fakecolt;
- private GameObject fakehammer;
- //player gameobject
- public GameObject player;
- //hand gameobject or what the gun goes to when it was created
- public GameObject hand;
- //were the coltprefab spawns when the player clicks q
- public GameObject dropit;
- //the fakecolt prefab that is in the players hand
- public GameObject fakecoltprefab;
- //the fakehammerofdeath prefab that is in the players hand
- public GameObject fakehammerprefab;
- //the colt that is in the ground and not in the players hand
- public GameObject coltprefab;
- //the hammerofdeath that is in the ground and not in the players hand
- public GameObject hammerprefab;
- //camera
- public GameObject hidingcamera;
- public Transform barrel;
- //public LayerMask layermask = -1;
- void Awake ()
- {
- source = GetComponent <AudioSource>();
- }
- void Start ()
- {
- numberofguns = 1;
- //find player
- player = GameObject.Find("Player");
- //gun named colt has never been created
- createdguncolt = true;
- createdhammer = true;
- }
- //if you collide guns, if you wanna create new ones just copy and paste with different tag
- void OnTriggerEnter(Collider other)
- {
- //if you collide with a gameobject with the tag "Colt"
- //If key E is down
- //guns[2] == 1 is the type of gun 2 as hand and 1 as the type of gun in hand
- if(other.gameObject.tag == "Colt" && (Input.GetKey(KeyCode.E) && !(guns[2] == 1)))
- {
- guns[2] = 1;
- collidedwithcolt = true;
- //player has collided with colt
- fakecolt = Instantiate(fakecoltprefab) as GameObject;
- fakecolt.transform.position = hand.transform.position;
- fakecolt.transform.parent = hand.transform;
- fakecolt.transform.rotation = player.transform.rotation;
- //create a fake colt in the players general direction
- Destroy (other.gameObject);
- //destorying the fake object
- Debug.Log("I have picked up a gun with the tag colt and pressed E");
- }
- else
- {
- collidedwithcolt = false;
- }
- if(other.gameObject.tag == "hammerofdeath" && (Input.GetKey(KeyCode.E) && !(guns[3] == 1)))
- {
- Debug.Log("Collided with hammer");
- guns[3] = 1;
- collidedwithhammer = true;
- //player has collided with colt
- fakehammer = Instantiate(fakehammerprefab) as GameObject;
- fakehammer.transform.position = hand.transform.position;
- fakehammer.transform.parent = hand.transform;
- fakehammer.transform.rotation = player.transform.rotation;
- //create a fake colt in the players general direction
- Destroy (other.gameObject);
- //destorying the fake object
- Debug.Log("I have picked up a gun with the tag hammer and pressed E");
- }
- else
- {
- collidedwithhammer = false;
- }
- //double checking if the colt is in the players hand
- if(other.gameObject.tag == "fakecolt")
- {
- doublecheckcolt = true;;
- }
- else
- {
- doublecheckcolt = false;
- }
- }
- IEnumerator FireColt ()
- {
- RaycastHit hit;
- Ray ray = new Ray(barrel.position, transform.forward);
- if(Physics.Raycast(ray, out hit, rangeofcolt))
- {
- if(hit.collider.tag == "enemy")
- {
- Enemy enemy = hit.collider.GetComponent<Enemy>();
- Debug.Log("getting enemy info");
- enemy.enemyhealth -= coltdamage;
- }
- }
- Debug.DrawRay(fakecolt.transform.position, transform.forward * rangeofcolt, Color.green);
- //yield return null;
- yield return new WaitForSeconds(coltbetweenshots);
- firedcolt = false;
- }
- IEnumerator FireHammer ()
- {
- RaycastHit hit;
- Ray hammerray = new Ray(barrel.position, transform.forward);
- if(Physics.Raycast(hammerray, out hit, rangeofcolt))
- {
- if(hit.collider.tag == "enemy")
- {
- Enemy enemy = hit.collider.GetComponent<Enemy>();
- Debug.Log("getting enemy info");
- enemy.enemyhealth -= hammerdamage;
- }
- }
- Debug.DrawRay(fakehammer.transform.position, transform.forward * rangeofcolt, Color.green);
- // yield return null;
- yield return new WaitForSeconds(hammerbetweenshots);
- firedhammer = false;
- }
- IEnumerator ReloadColt()
- {
- if (coltclip <= 0 && secondaryammo >= 8)
- {
- yield return new WaitForSeconds(reloadcolttime);
- coltclip += reloadcolt;
- secondaryammo -= reloadcolt;
- reloadedcolt = false;
- }
- }
- void Update ()
- {
- if (coltclip > 8)
- {
- coltclip = 8;
- }
- if (coltclip <= 0 && secondaryammo >= 8 && Input.GetKeyDown(KeyCode.R) && reloadedcolt == false)
- {
- reloadedcolt = true;
- StartCoroutine(ReloadColt());
- }
- //if ((Input.GetKey(KeyCode.Mouse1) && numberofguns == 2 && guns[2] == 1))
- // {
- // RaycastHit hit;
- // if (Physics.Raycast(transform.position,transform.forward, out hit, Mathf.Infinity, layermask))
- // {
- // hit.collider.SendMessage("Damage", damage,SendMessageOptions.DontRequireReceiver);
- // }
- //}
- // guns[2] == 1 is the type of gun 2 as hand and 1 as type of gun in hand
- // number of guns is which hand is held
- // key q is down to drop gun
- if ((Input.GetKey(KeyCode.Mouse0) && guns[2] == 1 && numberofguns == 2 && !firedcolt && coltclip > 0))
- {
- coltclip -= 1;
- firedcolt = true;
- Debug.Log("Starting fire");
- StartCoroutine("FireColt");
- ChooseColt = Random.Range (1,2);
- if (ChooseColt == 1)
- {
- float vol = Random.Range (VolumeLowRange, VolumeHighRange);
- source.PlayOneShot(ColtShoot, vol);
- Debug.Log ("Colt shoot");
- }
- if (ChooseColt == 2)
- {
- float vol = Random.Range (VolumeLowRange, VolumeHighRange);
- source.PlayOneShot(ColtShootSecond, vol);
- Debug.Log ("Colt Shoot 2nd");
- }
- }
- if ((Input.GetKey(KeyCode.Mouse0) && guns[3] == 1 && numberofguns == 3 && !firedhammer))
- {
- coltclip -= 1;
- firedhammer = true;
- Debug.Log("Starting fire");
- StartCoroutine("FireHammer");
- }
- if ((Input.GetKey(KeyCode.Q) && guns[2] == 1 && numberofguns == 2))
- {
- Debug.Log("I pressed q and will drop gun");
- guns[2] = 0;
- GameObject colt = Instantiate(coltprefab) as GameObject;
- colt.transform.position = dropit.transform.position;
- //Destroy(fakecolt);
- }
- if ((Input.GetKey(KeyCode.Q) && guns[3] == 1 && numberofguns == 3))
- {
- Debug.Log("I pressed q and will drop hammer");
- guns[3] = 0;
- GameObject hammer = Instantiate(hammerprefab) as GameObject;
- hammer.transform.position = dropit.transform.position;
- //Destroy(fakecolt);
- }
- if (guns[2] == 1 && numberofguns == 1)
- {
- //make it so you cant see the colt
- fakecolt.SetActive(false);
- }
- // if the gun colt in the array is active and number 3 is active
- if (guns[2] == 1 && numberofguns == 3)
- {
- //make it so you cant see the colt
- fakecolt.SetActive(false);
- }
- // if the gun colt in the array is active and number 2 is active
- if (guns[2] == 1 && numberofguns == 2)
- {
- //make it so you can see the colt
- fakecolt.SetActive(true);
- }
- //hammertime
- if (guns[3] == 1 && numberofguns == 1)
- {
- fakehammer.SetActive(false);
- }
- if (guns[3] == 1 && numberofguns == 2)
- {
- fakehammer.SetActive(false);
- }
- if (guns[3] == 1 && numberofguns == 3)
- {
- fakehammer.SetActive(true);
- }
- //checks if you are holding 2nd hand
- /*
- else
- {
- Destroy (fakecoltprefab);
- }
- */
- //set number of gun
- if (Input.GetKey(KeyCode.Alpha1))
- {
- //key 1 pressed
- numberofguns = 1;
- }
- if (Input.GetKey(KeyCode.Alpha2))
- {
- //key 2 pressed
- numberofguns = 2;
- }
- if (Input.GetKey(KeyCode.Alpha3))
- {
- //key 3 pressed
- numberofguns = 3;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement