- Home /
Gestures in javascript
Hello, i have a problem. I'm trying to create a simple game in Unity in which the character has to perform a gesture with his arm. I want to create the gestures with multiple collisions in a specific order. At the moment I have 5 spheres in a row which i want the character to activate from left to right. I tried to put booleans on every sphere and when the character collides with the sphere the boolean will be set to true. When enough booleans are set to true (in our case 3) the character has performed the gesture good enough.
In the beginning all booleans are set to false but when they are set true they turn back immediately.
This is our code: var aSound: AudioClip; var Sphere1 : boolean = false; var Sphere2 : boolean = false; var Sphere3 : boolean = false; var Sphere4 : boolean = false; var Sphere5 : boolean = false; var aantalGeraakt : int; function OnCollisionExit(theCollision : Collision){ if(gameObject.name == "Sphere1" && theCollision.gameObject.tag == "Rechts"){ Debug.Log("Hit Sphere1"); audio.PlayOneShot(aSound); Sphere1 = true; aantalGeraakt ++; } if(gameObject.name == "Sphere2" && theCollision.gameObject.tag == "Rechts"){ Debug.Log("Hit Sphere2"); audio.PlayOneShot(aSound); Sphere2 = true; aantalGeraakt ++; } if(gameObject.name == "Sphere3" && theCollision.gameObject.tag == "Rechts"){ Debug.Log("Hit Sphere3"); audio.PlayOneShot(aSound); Sphere3 = true; aantalGeraakt ++; } if(gameObject.name == "Sphere4" && theCollision.gameObject.tag == "Rechts"){ Debug.Log("Hit Sphere4"); audio.PlayOneShot(aSound); Sphere4 = true; aantalGeraakt ++; } if(gameObject.name == "Sphere5" && theCollision.gameObject.tag == "Rechts"){ Debug.Log("Hit Sphere5"); audio.PlayOneShot(aSound); Sphere5 = true; aantalGeraakt ++; } Debug.Log(aantalGeraakt); Debug.Log("blokkie 2: " +Sphere2+ " - " + Sphere3); return; }
I have the feeling that the whole script is in some sort of loop which resets all variables back to default.
Who knows how to solve this problem?
Thanks in advance
this code is attached to several gameObjects, right?
well in each script there will be different values for each variable...
So it means that the last script will write in console its contents of variables... I would prefer to use one script with a link to each gameObject, that will make your job easier and you will get correct boolean values.
Where do you set the Sphere booleans to false? (except the initialisation)
Answer by G_Sacristan · May 31, 2011 at 05:00 PM
this code is attached to several gameObjects, right?
well in each script there will be different values for each variable...
So it means that the last script will write in console its contents of variables... I would prefer to use one script with a link to each gameObject, that will make your job easier and you will get correct boolean values.
var spheres: GameObject[];//set size to 5 and add all spheres
var aantalGeraakt : int;
function OnCollisionExit(theCollision : Collision)
{
for(var i=0;i<spheres.length;i++)
{
if(spheres[i]==theCollision.gameObject&&theCollision.gameObject.tag == "Rechts")
{
audio.PlayOneShot(aSound);
aantalGeraakt ++;
}
}
}
if u need something more specific then ask and ill make code for you!
Hey SCape_games,
First off, I'd like to thank you for your help. You were right about the spheres having their own variables. I integrated the script you gave but there were some difficulties. On which gameObject do I have to connect the script to. Because when I connect it to the gameObject 'Sphere1' the collision only takes place with that single sphere. And if I connect the script to all of the spheres I have the same problem that each sphere has it's own variables. This is how the script looks like at this moment:
var spheres : GameObject[];
var aantalGeraakt : int; var aSound : AudioClip;
function OnCollisionExit(theCollision : Collision)
{
for(var i=0;i<spheres.length;i++)
{
if(spheres[i]==gameObject && theCollision.gameObject.tag == "Rechts"){
audio.PlayOneShot(aSound);
aantalGeraakt ++;
i++;
Debug.Log("i = " + i);
Debug.Log("aantalGeraakt = " + aantalGeraakt);
}
}
}
I also changed "spheres[i]==theCollision.gameObject" to "spheres[i]==gameObject" because the collision doesn't happen as it was. How do we give the spheres the same variables?
Thanks in advance.
Answer by G_Sacristan · Jun 01, 2011 at 12:50 PM
first of all add script to empty object (at least not on sphere), then
var spheres : GameObject[];
var aantalGeraakt : int;
var aSound : AudioClip;
function OnCollisionExit(theCollision: Collision)
{
for(var i=i;i<spheres.length;i++)
{
if(spheres[i]==theCollision.gameObject)//check if the collision gameObject matches one of spheres
{
print(Sphere: +spheres[i].name);//print which sphere got collision
audio.PlayOneShot(aSound);
aantalGeraakt++; //no need to increase index, because its already done in loop
}
}
}
If i understood corectly, u want to check if there is a collision with sphere and so u want to get which one collides Cheers!
Hello, here I am again. I tried everything you said and connected the script to an empty game object but for some reason I don't get any reaction from the spheres when the character's right hand (which is also a sphere and has the target 'Right') collides. It only works when the script is connected to the sphere itself. Do I have to add specific components to the spheres or to the hand to let the collision take place?
Answer by G_Sacristan · Jun 01, 2011 at 04:31 PM
i got an arror, sorry
var spheres : GameObject[];
var aantalGeraakt : int;
var aSound : AudioClip;
function OnCollisionExit(theCollision: Collision)
{
for(var i=0;i<spheres.length;i++)
{
if(spheres[i]==theCollision.gameObject)//check if the collision gameObject matches one of spheres
{
print(Sphere: +spheres[i].name);//print which sphere got collision
audio.PlayOneShot(aSound);
aantalGeraakt++; //no need to increase index, because its already done in loop
}
}
}
Answer by G_Sacristan · Jun 01, 2011 at 04:31 PM
i got an arror, sorry
var spheres : GameObject[];
var aantalGeraakt : int;
var aSound : AudioClip;
function OnCollisionExit(theCollision: Collision)
{
for(var i=0;i<spheres.length;i++)
{
if(spheres[i]==theCollision.gameObject)//check if the collision gameObject matches one of spheres
{
print(Sphere: +spheres[i].name);//print which sphere got collision
audio.PlayOneShot(aSound);
aantalGeraakt++; //no need to increase index, because its already done in loop
}
}
}