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 Solu9 · Oct 20, 2016 at 07:56 PM · scripting problemscripting beginnerscriptableobject

Selecting an ability based on UI image/button, and thereafter selecting a target for the ability

Hey guys and gals.

First of all this is going to be a long winded post so I hope you can stick with me.

I am trying to create a system where the player can select a unit he controls and by doing so the abilities that unit holds will show in the UI.

Hereafter I want the player to be able to click on the unit's abilities, and depending on the allowed targets for the clicked ability, be able to select at target (some abilities can also be instants).

Also, but this is secondary, a way to drag abilities from, let's say a spellbook, and attach it to a UI ability slot. Think of it a bit like how it's done in World of Warcraft.

So a short summary:

  • Player can select a unit of his/hers

  • Unit has abilities in UI ability slots

  • Player can select these abilities

  • Player can select a target for the selected ability

What in my head seemed like a task I could overcome by myself, it has shown a lot more difficult. I know this can prove to be a massive undertaking because my hand needs to be held all the way til the end. I will credit you in whatever I do in the future, from making dinner to getting a highscore in Bejeweled.

This is what I have so far (bare in mind this is how "I" have done it. But does absolutely not mean HOW I want it).

The ControllerPlayer script is right now attached to a GameObject in the scene. This script keeps track of what the player interacts with.

  using UnityEngine;
  using System.Collections;
  
  public class ControllerPlayer : MonoBehaviour {
  
      public int playerID;
  
      public GameObject char01;
      public GameObject char02;
      public GameObject char03;
  
      public KeyCode char01Hotkey;
      public KeyCode char02Hotkey;
      public KeyCode char03Hotkey;
  
      public GameObject selection;
      public GameObject abilitySelected; //I'm pretty sure this is a bad idea to have as GameObject
      public GameObject target;
      public GameObject hoverTarget = null;
      
      void Update(){
  
          //Select
          if (Input.GetKeyDown ((KeyCode.Alpha1))) {
              selection = char01;
          }
          if (Input.GetKeyDown ((KeyCode.Alpha2))) {
              selection = char02;
          }
          if (Input.GetKeyDown ((KeyCode.Alpha3))) {
              selection = char03;
          }
  
          //The target which the mouse points towards
          hoverTarget = MouseOver();
  
          //If left-clicking
          if (Input.GetKeyDown((KeyCode.Mouse0)))
          {
  
              if (hoverTarget != null)
              {
                  
                  if (hoverTarget.tag == "Ability") {
                      abilitySelected = hoverTarget;
                  }
                  
                  if((hoverTarget.tag  == "Enemy" || hoverTarget.tag  == "Character"))
                  {
                      if(abilitySelected == null)
                      {
                          selection = hoverTarget;
                      }
                      else
                      {
                          target = hoverTarget;
                      }
                  }
  
                  
                  if(abilitySelected != null)
                  {
  
                  }
                  
                  Debug.Log (hoverTarget.tag);
              }
          }
  
          //If right-clicking
          if (Input.GetKeyDown((KeyCode.Mouse1)))
          {
              if (selection != null)
              {
                  //If the player has selected an ability then deselect the ability
                  if(abilitySelected != null)
                  {
                      abilitySelected = null;
                  }
                  else
                  {
                      //Order the selected unit to move
                  }
              }
          }
      }
  
      public static GameObject MouseOver()
      {
          Ray rayHover = Camera.main.ScreenPointToRay(Input.mousePosition);
          
          RaycastHit hitHover;
          
          if (Physics.Raycast (rayHover, out hitHover, 1000f)) {
              return hitHover.collider.gameObject;
              
          }
          else
          {
              return null;
          }
      }
      
  }

And the draft for the base ability scriptable object from which all abilities will derived from:

 using UnityEngine;
  using System.Collections;
  
  public abstract class Ability_Base : ScriptableObject{
  
      public string abilityName = "New Ability";
      public int abilityID;
      
      public enum TargetType{NoTarget, Unit, Ground, Unit_Ground}
      public TargetType targetType;
      
      public enum DamageType{Physical, Fire, Ice, Electricity, Poison, Magic, NonDmg}
      public DamageType damageType;
      
      public float castTime;
      public float coolDownTime;
      public GameObject projectile;
      public float range;
      
      public abstract void Activate()
  }

I have thought about having two Ienumerators in the ability base script. One which keeps track if the ability is selected, and one to keep track of a suitable target has been found.

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

How do I make a photography function? 0 Answers

looking for a way to set an integer to another number based on a condition. 2 Answers

Beginner Question: How to get normals from a physics raycast using visual scripting? 0 Answers

OnMouseOver with 2 overlapping object not working properly 0 Answers

Make ScriptableObject assets customizable. 1 Answer


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