x


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

more ▼

asked May 31 '11 at 02:14 PM

bgacem gravatar image

bgacem
1 1 1 1

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.

May 31 '11 at 04:52 PM G_Sacristan

Where do you set the Sphere booleans to false? (except the initialisation)

May 31 '11 at 04:55 PM aldonaletto
(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

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 ++;
          }
     }
}
more ▼

answered May 31 '11 at 05:00 PM

G_Sacristan gravatar image

G_Sacristan
137 24 28 30

if u need something more specific then ask and ill make code for you!

May 31 '11 at 05:01 PM G_Sacristan

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.

Jun 01 '11 at 11:20 AM bgacem
(comments are locked)
10|3000 characters needed characters left

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!

more ▼

answered Jun 01 '11 at 12:50 PM

G_Sacristan gravatar image

G_Sacristan
137 24 28 30

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?

Jun 01 '11 at 02:04 PM bgacem
(comments are locked)
10|3000 characters needed characters left

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
         }
    }
}
more ▼

answered Jun 01 '11 at 04:31 PM

G_Sacristan gravatar image

G_Sacristan
137 24 28 30

(comments are locked)
10|3000 characters needed characters left

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
         }
    }
}
more ▼

answered Jun 01 '11 at 04:31 PM

G_Sacristan gravatar image

G_Sacristan
137 24 28 30

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3443
x239
x114
x24

asked: May 31 '11 at 02:14 PM

Seen: 1071 times

Last Updated: Jun 01 '11 at 04:31 PM