- Home /
Combo system based on collision
I have the following code. What it is supposed to do is print the identifier of each collider, and if the Array that contains the colliders of the 2 last seconds equals to a predefined combo array, print something else. It doesn't work. It prints EACH collider as expected, but never the combo. Here is my code.
var COMBO:Array;
static var MoveLeftHandCombo:Array=["A","B"];
static var MoveRightHandCombo:Array=["C","D"];
public var startime:float=0;
public var endtime:float=2;
private var thistime:float=0;
function Awake()
{
thistime=startime;
GEST_A=GameObject.Find("GEST_A");
GEST_B=GameObject.Find("GEST_B");
GEST_C=GameObject.Find("GEST_C");
GEST_D=GameObject.Find("GEST_D");
function Update()
{
if (thistime<endtime)
{
thistime+=Time.deltaTime;
}
else
{
COMBO=[];
}
if (COMBO==MoveLeftHandCombo)
{
Debug.Log("MoveLeftHandCombo");
}
else if (COMBO==MoveRightHandCombo)
{
Debug.Log("MoveRightHandCombo");
}
}
function OnTriggerEnter(coll:Collider)
{
if (coll.gameObject==GEST_A)
{
COMBO.push("A");
Debug.Log(COMBO);
}
if (coll.gameObject==GEST_B)
{
COMBO.push("B");
Debug.Log(COMBO);
}
if (coll.gameObject==GEST_C)
{
COMBO.push("C");
Debug.Log(COMBO);
}
if (coll.gameObject==GEST_D)
{
COMBO.push("D");
Debug.Log(COMBO);
}
}
Is CO$$anonymous$$BO being set to [] before the 2 second window? Experiment with getting the combo system to work with no time in order to rule out any logic problems. When you can hit both objects and get the correct combo message to fire then you should implement a timer on it.
Answer by Vonni · Feb 02, 2013 at 12:10 AM
Didn't know COMBO=[] worked :S Doesn't look like it would!
Also, im not even sure comparing two identical arrays like : if(array1 == array2) would even work. Because i don't think it checks the content ( i could be wrong here ).
Try debugging and remove timer.
I would also suggest switching from arrays to a string
//Maybe store this in an string[] builtin array ???
var SuperRightHandCombo : string = "abcd";
var NotSoGreatKickToTheFaceCombo : string = "bcbc";
//and then simply
var currentCombo : string;
if (coll.gameObject==GEST_A)
{
currentCombo += "a" // Or b, c, d, e, f ,g ,w
}
EDIT: TESTED Array comparison
var array1 : Array = [4,4,4,4];
var array2 : Array = [4,4,4,4];
function Start () {
if(array1 == array2)
print("They are alike!");
else
print("Not alike");
}
And it does NOT work.
EDIT 2: Regarding time and combo checking
Instead of declaring a time a combo needs to be executes within. Instead declare a time for which each input (a , b or c ... etc) has to be within oneanother.
import System.Collections.Generic;
class CombatInput {
var atTime : float;
var move : string;
}
var combatInput : List.< CombatInput > = new List.< CombatInput >();
var comboSpeed : float = 0.5;
var superAwesomeSpinAttack : string = "abcd";
if (coll.gameObject==GEST_A)
{
// Maybe make a function out of this these if statements so you dont have to type em over and over again for each input check
if(combatInput.Count != 0){ // checks if list is empty
// Checks the LATEST variable in your LIST and sees if it was more than comboSpeed (in time) ago.
if(combatInput[combatInput.Count].atTime + comboSpeed < Time.time){
combatInput.Clear();
}
}
combatInput.Add(CombatInput(Time.time, "a");
if(combatInput.Count == 4){
var currentCombo : string = "";
for(var i : int; i < combatInput.Count; i++){
currentCombo += combatInput[i].move;
}
if(currentCombo == superAwesomeSpinAttack){
// Do combo move somehow
}
}
}
This works without time! But I need time, so that it can clear if combo is not done in 2 secs. It doesn't work with my current setup.
Record the time of the last hit and add your interval to it. If that value becomes greater or equal to the current time then clear you list.
Upvote maybe if it worked for you. As far as time goes. You should be able to figure that out yourself.
Regarding iwaldrop's answer, how do you record the time of last hit?
I think you need to think a bit more about how that combo system will work. Because it seems to me like you want it to start some timer on the first hit, and then if at any point you do a sequence after that and within the time limit, the combo would execute. But there might be a problem with that. SAY, someone wants to do the combo = abcd. But does a simple jab with the "b" to hit the target. THAT would start the timer... and whatever is done after that will be babcd, and simply would not start the combo. The system needs to be more complex. EXA$$anonymous$$PLE: you hit bbbabcdbaabc over 5 seconds. That might make the CURRENT combo: bbbab and cdba amd abc... none which make you do any combo. You need a system that tracks all the input (a,b,c etc.. d a) and then has some way of checking if a sequence matches the combo AND are all without 2 seconds of oneanother.
UPDATED ANSWER WITH A SOLUTION.
Your answer
Follow this Question
Related Questions
help with fruit ninja like slice combo 1 Answer
help with scripting combo points 1 Answer
collision combo 1 Answer