Download Link
Code Snippets
-
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MenuScript : MonoBehaviour
{
public enum Weapons
{
Pistol, //T0
ChargedPistol, //T1
KnockbackAR, //T1
BubbleShotgun, //T2
ChickenGun, //T2
WobblyLaserSniper, //T3
ExplosiveMelonGun, //T3
GravityGun, //T4
}
private Weapons weapons;
private Vector2 normalizedMousePosition;
private float currentAngle;
private int selection, previousSelection;
private GameObject previousWeapon;
[SerializeField] private Canvas canvas;
[SerializeField]
public GameObject pistol, ar, chickenGun, chargePistol,
bubbleShotgun, Lasersniper, waterMelonGun, gravityGun;
[SerializeField] private GameObject[] menuItems;
private MenuItems menuItem, previousMenuItem;
public Action<GameObject> onSwitch;
#region GAME_EVENTS
private void OnEnable()
{
void GetImageEvent(string image) => GameObject.Find(image).GetComponent<Image>().enabled = true;
GameEvents.bubbleShotgunUnlock += (b) => GetImageEvent("Bubbleshotgun");
GameEvents.knockbackARUnlock += (b) => GetImageEvent("Knockback");
GameEvents.chargePistolUnlock += (b) => GetImageEvent("chargingPistol");
GameEvents.chickenGunUnlock += (b) => GetImageEvent("ChickenGunImage");
GameEvents.laserSniperUnlock += (b) => GetImageEvent("wobblyLaserSniperImage");
GameEvents.explosiveMelonGunUnlock += (b) => GetImageEvent("explosiveMelonGunImage");
GameEvents.gravityGunUnlock += (b) => GetImageEvent("gravityGunImage");
}
#endregion
private void OnDisable()
{
Maintain.SetNull();
}
public static MenuScript Menu { get; set; }
private void Awake()
{
if (Menu == null)
Menu = this;
else Destroy(gameObject);
}
private void Start() => previousWeapon = pistol;
public void SwapToWeapon(GameObject weapon)
{
if (weapon != null)
if (weapon.CompareTag("Unlocked"))
{
previousWeapon.SetActive(false);
previousWeapon = weapon;
onSwitch?.Invoke(weapon);
weapon.SetActive(true);
}
}
void Update()
{
if (canvas.enabled)
{
//Creates a asis stelsel met x-0 en y-0
normalizedMousePosition = new Vector2(Input.mousePosition.x - Screen.width / 2, Input.mousePosition.y - Screen.height / 2);
//Gets the angle of the mouse using Ata2 and then transforms it to degrees
currentAngle = Mathf.Atan2(normalizedMousePosition.y, normalizedMousePosition.x) * Mathf.Rad2Deg;
//Rotates the asis stelsel to make it start from 1 to 8 clockwise
currentAngle = (currentAngle - 450) % -360;
selection = (int)currentAngle / -45;
if (selection != previousSelection && selection >= 0 && selection < 8)
{
previousMenuItem = menuItems[previousSelection].GetComponent<MenuItems>();
previousMenuItem.Deselect();
previousSelection = selection;
menuItem = menuItems[selection].GetComponent<MenuItems>();
menuItem.Select();
WeaponSwap();
}
}
}
private void WeaponSwap()
{
weapons = (Weapons)selection;
switch (weapons)
{
case Weapons.Pistol:
SwapToWeapon(pistol);
break;
case Weapons.ChargedPistol:
SwapToWeapon(chargePistol);
break;
case Weapons.KnockbackAR:
SwapToWeapon(ar);
break;
case Weapons.BubbleShotgun:
SwapToWeapon(bubbleShotgun);
break;
case Weapons.ChickenGun:
SwapToWeapon(chickenGun);
break;
case Weapons.WobblyLaserSniper:
SwapToWeapon(Lasersniper);
break;
case Weapons.ExplosiveMelonGun:
SwapToWeapon(waterMelonGun);
break;
case Weapons.GravityGun:
SwapToWeapon(gravityGun);
break;
default:
SwapToWeapon(pistol);
break;
}
}
}
-
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MenuItems : MonoBehaviour
{
[SerializeField] private Color baseColor, hoverColor, disabledColor;
[SerializeField] private Image background;
private bool disabled;
void Start() => background.color = baseColor;
public void Select()
{
// if (!disabled)
background.color = hoverColor;
}
public void Deselect()
{
// if (!disabled)
background.color = baseColor;
}
}
-
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AR : Gun, IHitscan
{
public int knockback = 1000;
public override void Awake()
{
base.Awake();
}
// Start is called before the first frame update
public override void Start()
{
reloadTime = 2;
maxAmmo = 26;
shootTimer = 0.5f;
bulletDamage = 15;
base.Start();
}
// Update is called once per frame
public override void Update()
{
base.Update();
if (NextShotReady())
{
animator.SetTrigger("Idle");
}
if (Input.GetKey(KeyCode.Mouse0) && NextShotReady())
{
animator.SetTrigger("Shooting");
AudioManager.Sound.Play(shootSound);
StartCoroutine(Shoot(camera.transform.position, camera.transform.forward));
muzzleFlash.Play();
StartCoroutine(ShootCooldown(shootTimer));
}
}
public IEnumerator Shoot(Vector3 startPosition, Vector3 direction)
{
direction.Normalize();
RaycastHit shot;
if (Physics.Raycast(startPosition, direction, out shot))
{
GameEvents.shot?.Invoke(shot, (int)bulletDamage);
//Gets the target component using the raycast, does dmg and adds a force to the object the raycast deteted(must have a rigidbody)
Target target = shot.transform.GetComponent<Target>();
GiveDamage(target);
if (shot.rigidbody !=null)
{
shot.rigidbody.AddForce(-shot.normal * knockback*2,ForceMode.Impulse);
}
}
currentAmmo--;
yield return new WaitForEndOfFrame();
}
}