- Home /
help with fruit ninja like slice combo
Hello. I am trying to script a combo where multiple prefabs tossed in the air like fruit ninja are sliced in one swipe. I want that when the swipe gameobject collides with three or more prefabs this creates a combo and then a "combo" prefab is spawned at the position of the last hit prefab saying 3,4, or 5 hit combo based on the number of prefabs hit with one swipe. I have no clue as to how to do this, and everything i tried does not seem to work.
This is what i have tried: in pseudocode - when the collision occurs with the swipe gameobject and the prefab, save the locations of the collisions in a list, then check the number of collisions which occurred in 1 second, which is the timeframe for the combo, and then spawn the correct combo gameobject at the last collision point based on the number of hits within the timeframe.
I tried this code below but could not get it to work. PLEASE HELP??
var myHitList : List.<Vector3> = new List.<Vector3>();
var hits : Collider2D[];
var combo : GameObject;
var combo1 : GameObject;
var combo2 : GameObject;
var combo3 : GameObject;
var combo4: GameObject;
var hit : float;
function OnCollisionEnter2D(coll: Collision2D) {
if (coll.gameObject.tag == "slice" && myHitList.Count == 0){
Invoke ("Do", 1);
Turn();
}
if (coll.gameObject.tag == "slice"){
myHitList.Add(coll.gameObject.transform.position);
}
if(hit == 3)
{
Instantiate(combo, myHitList[myHitList.Count-1], Quaternion.identity);
Instantiate(combo1, myHitList[myHitList.Count-1], Quaternion.identity);
}
else if(hit == 4)
{
Instantiate(combo, myHitList[myHitList.Count-1], Quaternion.identity);
Instantiate(combo2, myHitList[myHitList.Count-1], Quaternion.identity);
}
else if(hit == 5)
{
Instantiate(combo, myHitList[myHitList.Count-1], Quaternion.identity);
Instantiate(combo3, myHitList[myHitList.Count-1], Quaternion.identity);
}
else if(hit == 6)
{
Instantiate(combo, myHitList[myHitList.Count-1], Quaternion.identity);
Instantiate(combo4, myHitList[myHitList.Count-1], Quaternion.identity);
}
}
function Update() {
}
function Turn (){
yield WaitForSeconds (1.2);
myHitList.Clear();
hit=0;
CancelInvoke("Do");
}
function Do (){
hit = myHitList.Count;
}
Answer by Eluate · Jul 19, 2014 at 05:27 AM
WaitForSeconds() requires you to use the StartCoroutine method.
function OnCollisionEnter2D(coll: Collision2D) {
if (coll.gameObject.tag == "slice" && myHitList.Count == 0){
Invoke ("Do", 1);
StartCoroutine(Turn());
}
if (coll.gameObject.tag == "slice"){
myHitList.Add(coll.gameObject.transform.position);
}
if(hit == 3)
{
Instantiate(combo, myHitList[myHitList.Count-1], Quaternion.identity);
Instantiate(combo1, myHitList[myHitList.Count-1], Quaternion.identity);
}
else if(hit == 4)
{
Instantiate(combo, myHitList[myHitList.Count-1], Quaternion.identity);
Instantiate(combo2, myHitList[myHitList.Count-1], Quaternion.identity);
}
else if(hit == 5)
{
Instantiate(combo, myHitList[myHitList.Count-1], Quaternion.identity);
Instantiate(combo3, myHitList[myHitList.Count-1], Quaternion.identity);
}
else if(hit == 6)
{
Instantiate(combo, myHitList[myHitList.Count-1], Quaternion.identity);
Instantiate(combo4, myHitList[myHitList.Count-1], Quaternion.identity);
}
}
Hi. Thanks for your response. I entered the startcoroutine into the code as you suggested, however I'm still getting some issues with this. When three prefabs are hit right now nothing happens. Do you think there is a better way to accomplish this other than the code I tried?