Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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

Character Controller with multiple state machines 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges