• 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 F4LL_Legion_Dev · Jun 30, 2020 at 05:36 AM · uiscript.buttonscombo

How to convert a Mouse Input Combo System to Button?

Hi, I have a combo system from Colenderp's video. I was wondering how I'd convert it from inputs to UI Button inputs? So instead of clicking inputs on the mouse, i want to click a button. Here is the video: https://www.youtube.com/watch?v=JV-PjU4q2EE

Here is the code:

 using System.Collections;
 using System.Collections.Generic;
 using System.Runtime.InteropServices;
 using UnityEngine;
 using UnityEngine.Events;
 using UnityEngine.UI;
 
 public enum AttackType {  heavy = 0, light = 1};
 public class fightingCombo : MonoBehaviour
 {
     [Header("Attacks")]
     public Attack heavyAttack;
     public Attack lightAttack;
     public List<Combo> combos;
     public float comboLeeway = 0.25f;
 
     Attack curAttack = null;
     ComboInput lastInput = null;
     List<int> currentCombos = new List<int>();
     float timer = 0;
     float leeway = 0;
     bool skip = false;
 
     void PrimeCombos()
     {
         for (int i = 0; i < combos.Count; i++)
         {
             Combo c = combos[i];
             c.onInputted.AddListener(() =>
             {
                 //call funtion for combos attack
                 skip = true;
                 Attack(c.comboAttack);
                 ResetCombos();
             });
         }
     }
 
     void Update()
     {
         if (curAttack != null)
         {
             if (timer > 0)
             {
                 timer -= Time.deltaTime;
             }
             else
             {
                 curAttack = null;
             }
             return;
         }
 
         if (currentCombos.Count > 0)
         {
             leeway += Time.deltaTime;
             if (leeway >= comboLeeway)
             {
                 if (lastInput != null)
                 {
                     Attack(getAttackFromType(lastInput.type));
                     lastInput = null;
                 }
                 ResetCombos();
             }
         }
         else
         {
             leeway = 0;
         }
 
         ComboInput input = null;
         if (Input.GetMouseButtonDown(1))
             input = new ComboInput(AttackType.heavy);
         if (Input.GetMouseButtonDown(0))
             input = new ComboInput(AttackType.light);
 
         if (input == null) return;
         lastInput = input;
 
         List<int> remove = new List<int>();
         for (int i = 0; i < currentCombos.Count; i++)
         {
             Combo c = combos[currentCombos[i]];
             if (c.continueCombo(input))
                 leeway = 0;
             else
             {
                 remove.Add(i);
             }
         }
 
         if (skip)
         {
             skip = false;
             return;
         }
 
         for(int i = 0; i < combos.Count; i++)
         {
             if (currentCombos.Contains(i)) continue;
             if (combos[i].continueCombo(input))
             {
                 currentCombos.Add(i);
                 leeway = 0;
             }
                 
         }
 
         foreach (int i in remove)
             currentCombos.RemoveAt(i);
         if (currentCombos.Count <= 0)
             Attack(getAttackFromType(input.type));
     }
 
     void ResetCombos()
     {
         leeway += Time.deltaTime;
         for (int i = 0; i < currentCombos.Count; i++)
         {
             Combo c = combos[currentCombos[i]];
             c.ResetCombo();
         }
 
         currentCombos.Clear();
     }
     
     void Attack(Attack att)
     {
         curAttack = att;
         timer = att.length;
     }
 
     Attack getAttackFromType(AttackType t)
     {
         if (t == AttackType.heavy)
             return heavyAttack;
         if (t == AttackType.light)
             return lightAttack;
         return null;
     }
 
 }
 
 [System.Serializable]
 public class Attack
 {
     public string name;
     public float length;
 }
 
 [System.Serializable] 
 public class ComboInput
 {
     public AttackType type;
 
     public ComboInput(AttackType t)
     {
         type = t;
     }
 
     public bool isSameAs(ComboInput test)
     {
         return (type == test.type);
     }
 }
 
 [System.Serializable] 
 public class Combo
 {
     public List<ComboInput> inputs;
     public Attack comboAttack;
     public UnityEvent onInputted;
     int curInput = 0;
 
     public bool continueCombo(ComboInput i)
     {
         if(inputs[curInput].isSameAs(i))
         {
             curInput++;
             if(curInput >= inputs.Count) //finished inputs and shld do attack
             {
                 onInputted.Invoke();
                 curInput = 0;
             }
             return true;
         }
         else
         {
             curInput = 0;
             return false;
         }
     }
 
     public ComboInput currentComboInput()
     {
         if (curInput >= inputs.Count) return null;
         return inputs[curInput];
     }
 
     public void ResetCombo()
     {
         curInput = 0;
     }
 }

Sorry for the long code, but ive been looking into this for a while now, with no help. Any help would be very greatly appreciated.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by BenWiller1989 · Aug 15, 2020 at 01:56 PM

Well, why should it be a problem, to have a reference of a method from this script on a UI Button, changing a Variable (a bool perhaps) to indicate the Input on the UI Button for your Script? I don't understand your issue.... You know how UI buttons work right? Br

Comment
Add comment · Share
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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

241 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

CompareTag Between multiple Buttons 0 Answers

Input Fields - Navigating with tab 1 Answer

How to make UI buttons so my player can move along certain lines 0 Answers

How to make one Inputfield Logon 1 Answer

How to set up UI button automatic navigation using keyboard but not using arrow keys or awsd 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges