- Home /
Question by
Kamaji63 · Oct 04, 2016 at 05:14 AM ·
programmingdamageenumswitch-case
Creating different damage types with enums switches
So I am using an enum and switch to pass through different damage values and status that affect the enemies in the different way. For example I want to shoot a fire enemy with a water attack but if I shoot the wrong element it will have no effect on an enemy. I'm not too sure how to go about passing value through the enum, still learning the ropes of programming.
This is my shooting script:
using UnityEngine;
using UnityEngine.UI; //This line is needed to use any UI elements such as "Text".
using System.Collections;
using System.Collections.Generic; //This line is needed to use the List array type
public class WeaponFire : MonoBehaviour
{
/* PUBLIC VARIABLES */
public Texture2D myCursorImg = null; //The reticule image
static public List<GameObject> enemyList = new List<GameObject>(); //The list of all the enemies in the level
public float weaponRange = 0.0f; //How far away enemies can be from the camera and still die
//public Text uiText = null; //Where the player score will be printed out.
//public string scoreString = "\0"; //The string that's printed before the score value;
public int damageValue = 0;
public AudioClip[] playAudio;
AudioSource audioPlay;
private WeaponStatusEffect statusEffect;
public GameObject[] weps;
private AIBehaviourMelee meleeAI;
//private AIBehaviourRanged rangedAI;
/* PRIVATE VARIABLES */
//private int playerScore = 0; //The player's current score
void Awake()
{
ChangeWep(0); //Set the default weapon to the first element slot.
statusEffect = GetComponent<WeaponStatusEffect>();
meleeAI = GetComponent<AIBehaviourMelee>();
/* JOB 1: We need to set the cursor image to the reticule */
//First, we need to find the exact position on the reticule image which counts as the "click".
//For most games, this is the center of the reticule, which we can find by dividing the length and height by half each.
Vector2 mouseCent = new Vector2(myCursorImg.width / 2.0f, myCursorImg.height / 2.0f);
//Now we can set the cursor image using our position we figured above. Ignore CursorMode.
Cursor.SetCursor(myCursorImg, mouseCent, CursorMode.Auto);
//We need to also initialise our score text to the default '0' value.
//uiText.text = scoreString + playerScore;
//Audio
//audioPlay = GetComponent<AudioSource>();
//Get all the enemies in the scene and put them into our static list for the health system to use
enemyList.Clear();
GameObject[] tempEnList = GameObject.FindGameObjectsWithTag("Enemy");
for (int i = 0; i < tempEnList.Length; i++)
{
enemyList.Add(tempEnList[i]);
Debug.Log(tempEnList[i].name + " added to enemyList.");
}
}
void Update()
{
// Slow time down.
if (Input.GetKeyDown(KeyCode.Z))
{
Debug.Log("Z has been pressed. Slow time down.");
if (Time.timeScale == 1.0f) Time.timeScale = 0.5f;
else
Time.timeScale = 1.0f;
Time.fixedDeltaTime = 0.02f * Time.timeScale;
}
// Swapping weapons and referencing the weapon status scripts. This manages the weapon swapping as well as inputting all the damage values
// from the other script. Otherwiswe trhe damage will be calculated in this script or a new one.
if (Input.GetKeyDown(KeyCode.L))
{
ChangeWep(0);
statusEffect.presetType = WeaponStatusEffect.WeaponPresetType.Logic;
}
if (Input.GetKeyDown(KeyCode.K))
{
ChangeWep(1);
statusEffect.presetType = WeaponStatusEffect.WeaponPresetType.Love;
}
if (Input.GetKeyDown(KeyCode.J))
{
ChangeWep(2);
statusEffect.presetType = WeaponStatusEffect.WeaponPresetType.Empathy;
}
if (Input.GetKeyDown(KeyCode.Escape)) Application.Quit(); //Quit the game.
/* JOB 2: Every update, we need to check if the player has successfully shot an enemy */
//For the future, we should add stuff that lets us add a little sway to the gun, reloading and so on.
//First, check if there's enemies in the level and that the player has clicked the button down.
if (enemyList.Count > 0 && Input.GetMouseButtonDown(0))
{
Debug.Log("I AM SHOOTING"); //Test to see if we are shooting.
Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition); //We need to get where the player clicked with the mouse.
RaycastHit hitInfo; //hitInfo is what will eventually save the name of whatever our mouse has clicked, which we need
//We need to make two checks; we need to first Raycast to check if we touched ANYTHING, and then we need to verify it's actually an enemy inside of our enemyList.
if (enemyList.Count > 0 && Physics.Raycast(mouseRay, out hitInfo, weaponRange) && enemyList.Contains(hitInfo.collider.gameObject))
{
hitInfo.collider.gameObject.GetComponent<HealthSystem>().Health -= damageValue;
//Finally, we can remove the enemy from the list and then destroy the game object in the world.
//enemyList.Remove(hitInfo.collider.gameObject);
//Destroy(hitInfo.collider.gameObject);
//Audio - See if we killed something play some audio. [Has to be wearing earphones/headphones or playing through speakers.]
//audioPlay.clip = playAudio[0];
//audioPlay.Play();
//Debug.Log("Audio is playing");
Debug.Log("I KILLED SOMETHING!!"); //Test to see if we killed something.
//Now update the player's score and the UI
//playerScore++;
//uiText.text = scoreString + playerScore.ToString();
}
}
}
public void ChangeWep(int seq)
{
disableAll();
weps[seq].SetActive(true);
}
public void disableAll()
{
foreach(GameObject wep in weps)
{
wep.SetActive(false);
}
}
}
This is the damage preset script:
using UnityEngine;
using System.Collections;
public class WeaponStatusEffect : MonoBehaviour
{
public enum WeaponPresetType { Logic, Love, Empathy }
public WeaponPresetType presetType;
void Update()
{
switch (presetType)
{
case WeaponPresetType.Logic:
Debug.Log("Logic cannon seleceted");
break;
case WeaponPresetType.Love:
Debug.Log("Bow of Enchament seleceted");
break;
case WeaponPresetType.Empathy:
Debug.Log("Bottle 'O' Feels seleceted");
break;
default:
Debug.Log("No weapon equipped or in array");
break;
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612093948im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Problem with getting a value from a enum 2 Answers
Grabbing an Inherited Variable 0 Answers
What is a more efficient way to write this Switch Statement? 3 Answers