• 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
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

People who like this

0 Show 0
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

  • Sort: 
avatar image

Answer by Hobby-Game-Developer · 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

People who like this

0 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

240 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

Related Questions

How to make the UI button (for phones) mean like on the keyboard "w" or "S" 0 Answers

CompareTag Between multiple Buttons 0 Answers

Shoot accuracy bar 1 Answer

Creating working UI Button from Script? 1 Answer

Buttons stopped working after I temporarily changed the font. 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