KeyCombo

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Text replace - "<javascript>" to "<syntaxhighlight lang="javascript">")
m (Text replace - "</javascript>" to "</syntaxhighlight>")
Line 38: Line 38:
 
}
 
}
 
}
 
}
}</javascript>
+
}</syntaxhighlight>
  
  
Line 57: Line 57:
 
Debug.Log("KICK");
 
Debug.Log("KICK");
 
}
 
}
}</javascript>
+
}</syntaxhighlight>
  
 
'''This is C# version (AmIr_bobo)'''
 
'''This is C# version (AmIr_bobo)'''

Revision as of 20:52, 10 January 2012

This class makes it easy to detect multi-key combos in, for example, fighting games. Created by StarManta.

Javascript: KeyCombo.js

class KeyCombo
{
	var buttons : String[];
	private var currentIndex : int=0; //moves along the array as buttons are pressed
	var allowedTimeBetweenButtons : float = 0.3; //tweak as needed
	private var timeLastButtonPressed : float;
 
	function KeyCombo(b : String[])
	{
		buttons = b;
	}
 
	//usage: call this once a frame. when the combo has been completed, it will return true
	function Check() : boolean
	{
		if (Time.time > timeLastButtonPressed + allowedTimeBetweenButtons) currentIndex=0;
		if (currentIndex < buttons.length)
		{
			if ((buttons[currentIndex] == "down" && Input.GetAxisRaw("Vertical") == -1) ||
			(buttons[currentIndex] == "up" && Input.GetAxisRaw("Vertical") == 1) ||
			(buttons[currentIndex] == "left" && Input.GetAxisRaw("Vertical") == -1) ||
			(buttons[currentIndex] == "right" && Input.GetAxisRaw("Horizontal") == 1) ||
			(buttons[currentIndex] != "down" &&  buttons[currentIndex] != "up" &&  buttons[currentIndex] != "left" &&  buttons[currentIndex] != "right" && Input.GetButtonDown(buttons[currentIndex])) )
			{
				timeLastButtonPressed = Time.time;
				currentIndex++;
			}
 
			if (currentIndex >= buttons.length)
			{
				currentIndex = 0;
				return true;
			}
			else return false;
		}
	}
}


Javascript Implementation (No filename requirement)

private var falconPunch : KeyCombo = KeyCombo(["down","right","right"]);
private var falconKick : KeyCombo = KeyCombo(["down", "right", "Fire1"]);
 
function Update()
{
	if (falconPunch.Check())
	{
		// do the falcon punch
		Debug.Log("PUNCH");	
	}
	if (falconKick.Check())
	{
		// do the falcon kick
		Debug.Log("KICK");
	}
}

This is C# version (AmIr_bobo)

using UnityEngine;
public class KeyCombo
{
    public string[] buttons;
    private int currentIndex = 0; //moves along the array as buttons are pressed
 
    public float allowedTimeBetweenButtons = 0.3f; //tweak as needed
    private float timeLastButtonPressed;
 
    public KeyCombo(string[] b)
    {
        buttons = b;
    }
 
    //usage: call this once a frame. when the combo has been completed, it will return true
    public bool Check()
    {
        if (Time.time > timeLastButtonPressed + allowedTimeBetweenButtons) currentIndex = 0;
        {
            if (currentIndex < buttons.Length)
            {
                if ((buttons[currentIndex] == "down" && Input.GetAxisRaw("Vertical") == -1) ||
                (buttons[currentIndex] == "up" && Input.GetAxisRaw("Vertical") == 1) ||
                (buttons[currentIndex] == "left" && Input.GetAxisRaw("Vertical") == -1) ||
                (buttons[currentIndex] == "right" && Input.GetAxisRaw("Horizontal") == 1) ||
                (buttons[currentIndex] != "down" && buttons[currentIndex] != "up" && buttons[currentIndex] != "left" && buttons[currentIndex] != "right" && Input.GetButtonDown(buttons[currentIndex])))
                {
                    timeLastButtonPressed = Time.time;
                    currentIndex++;
                }
 
                if (currentIndex >= buttons.Length)
                {
                    currentIndex = 0;
                    return true;
                }
                else return false;
            }
        }
 
        return false;
    }
}

Usage:

using UnityEngine;
using System.Collections;
 
public class Test : MonoBehaviour {
 
	private KeyCombo falconPunch= new KeyCombo(new string[] {"down", "right","right"});
	private KeyCombo falconKick= new KeyCombo(new string[] {"down", "right","Fire1"});
 
	void Update () {
		if (falconPunch.Check())
		{
			// do the falcon punch
			Debug.Log("PUNCH"); 
		}		
		if (falconKick.Check())
		{
			// do the falcon punch
			Debug.Log("KICK"); 
		}
	}
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox