using UnityEngine; using System.Collections; using UnityEngine.UI; public class Actions : MonoBehaviour { public GameObject[] abilities = new GameObject[4]; // The array full of the character's abilities public GameObject currentAbility = null; // Which ability is currently active private int abilitiesCounter = 0; // A counter which lets you cycle through the abilities private Image abilitiesImage; // Picture which shows which ability it active private Color abilityUnusable; private Color abilityUsable; public int fireballSpeed = 5; public bool waterShieldUsable = true; private float waterShieldTimer = 0.0f; public float waterShieldCooldown = 5.0f; private Vector3 LastInput = new Vector2(0, -1); void Start () { abilitiesImage = GameObject.FindGameObjectWithTag ("Cycle").GetComponent(); // Set the initial ability to something so that it's not null CycleAbility(); abilityUnusable = new Color(255.0f,255.0f,255.0f,0.5f); abilityUsable = new Color(255.0f,255.0f,255.0f,1.0f); } void Update () { RaycastHit2D hit = Physics2D.Raycast(transform.position, GetLastDirection()); Debug.DrawRay(transform.position, GetLastDirection(), Color.red); // Constantly check where the player is facing GetLastDirection(); if(Input.GetButtonDown("Fire1")) { UseAbility(currentAbility.name); } if(waterShieldUsable == false) { waterShieldTimer += Time.deltaTime; if(waterShieldTimer > waterShieldCooldown) { waterShieldUsable = true; waterShieldTimer = 0.0f; } abilitiesImage.GetComponent().color = abilityUnusable; } else { abilitiesImage.GetComponent().color = abilityUsable; } // If the player presses the button to choose an ability... if(Input.GetButtonDown("Fire2")) { // Increment the counter abilitiesCounter++; // Call the ChooseAbility() function CycleAbility(); } } void CycleAbility() { // If the counter goes out of range of the array's size, reset it to 0 if(abilitiesCounter > abilities.Length - 1) { abilitiesCounter = 0; } // Set the currentAbility to the one chosen by the abilitiesCounter currentAbility = abilities[abilitiesCounter]; // Set the image of the gameObject of the ability equal to the picture of the currentAbility abilitiesImage.GetComponent().sprite = currentAbility.GetComponent().sprite; } public void UseAbility(string myCurrentAbility) { switch(myCurrentAbility) { case "Ability - Fireball": GameObject clone = Instantiate (currentAbility, this.transform.position, Quaternion.identity) as GameObject; clone.GetComponent().move = LastInput; clone.GetComponent().speed = fireballSpeed; break; case "Ability - Water Shield": if(waterShieldUsable == true) { GameObject myShield = (GameObject)Instantiate(currentAbility, transform.position, Quaternion.identity); WaterShield waterShield = myShield.GetComponent(); waterShieldUsable = false; } break; case "Ability - Rock": Debug.Log("Throwing a rock!"); break; case "Ability - Breath of Air": Debug.Log("Casting breath of air!"); break; } } public Vector3 GetLastDirection() { Vector3 input = new Vector3 (Input.GetAxisRaw ("Horizontal"), Input.GetAxisRaw ("Vertical"), 0); if (input.x == 0 && input.y == 0) { //do nothing } else if (input.y != 0) { LastInput = new Vector3 (0, input.y, 0); } else { LastInput = new Vector3 (input.x, 0, 0); } return LastInput; } }