Last active
May 1, 2019 01:30
-
-
Save SenpaiRar/5d4efc586e1407fa0de9b1ffc2c9b241 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
[RequireComponent(typeof(AudioSource))] | |
public class Weapon_Parent : MonoBehaviour { | |
public AudioSource Source; | |
//Player weapon uses the Weapon_Template Scriptable object to get data for the weapon | |
public Weapon_Template Weapon_Data; | |
public bool FireEnabled; | |
[HideInInspector] | |
public int currentAmmo; | |
void Start () { | |
currentAmmo = Weapon_Data.StartingAmmo; | |
FireEnabled = true; | |
} | |
// Update is called once per frame | |
void Update () { | |
if(Input.GetKeyUp(Weapon_Data.key) && FireEnabled){ | |
if(currentAmmo > 0){ | |
OnFire(); | |
currentAmmo -= 1; | |
} | |
if(currentAmmo <= 0){ | |
Source.PlayOneShot(Weapon_Data.EmptySound); | |
} | |
} | |
} | |
void OnFire() | |
{ | |
Ray BulletDirection = GetBulletDirection(); | |
RaycastHit info; | |
Physics.Raycast(BulletDirection, out info); | |
Debug.DrawRay(BulletDirection.origin, BulletDirection.direction * 10, Color.blue, .5f); | |
Source.PlayOneShot(Weapon_Data.FireSound); | |
if (info.collider.gameObject.GetComponent<Entity_Parent>() != null) | |
info.collider.gameObject.GetComponent<Entity_Parent>().LoseHealth(Weapon_Data.Damage); | |
} | |
/// <summary> | |
/// Returns a ray extending from the current Player's position to the position the cursor is hovering over. | |
/// </summary> | |
/// <returns></returns> | |
public Ray GetBulletDirection() //Uses Player_Cursor function and the transform.position of the player | |
{ | |
Vector3 currentMouseRay = Player_Cursor.GetPositionClick(GameObject.Find("Main Camera").GetComponent<Camera>()); | |
Ray Ray = new Ray(transform.position, currentMouseRay - transform.position); | |
return (Ray); | |
} | |
public void PickUpWeapon(Weapon_Template newWeaponData) //Changes the current weapon data | |
{ | |
if(newWeaponData == Weapon_Data) //Adds ammo to current ammo if the same weapon | |
{ | |
currentAmmo += newWeaponData.StartingAmmo; | |
} | |
else //Changes ammo if a different weapon | |
{ | |
Weapon_Data = newWeaponData; | |
currentAmmo = newWeaponData.StartingAmmo; | |
} | |
} | |
public void ChangeFireStatus(bool NewStatus){ | |
FireEnabled = NewStatus; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment