- Home /
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.