Triggers and Collisions

Hi folks,

So I created a ball game where balls go through tubes, and that scores points!

What I want to know is that is there a way for me to create a “block” so that when 6 balls have passed the trigger, the tube closes up so no more balls can go into it?

Here’s how I imagine the code…

  • The balls themselves have an assigned number… so each balls has the value of “1”
  • The “blocker” is made invisible (which I can enable false)
  • When 6 balls pass through the blocker, the blocker activates and enables true (so now no more balls can go into the tube)

Here’s a drawing of what it looks like!

I know what I want to code…it’s just I don’t know which codes to write! or how to even go about it (I had a go but it’s all giving errors)

Many thanks,
Richard Curtis

Hmm… For something like this, I would have a moving block that both blocks passage way and also move the ball into a tray area:

So the blue blocks the hole, slides open (left in this case) so a ball can fall into it. When it does, the ball itself blocks the hole until the blue block pushes the ball out towards the collection tray and reblocks the hole. This ensures one ball is dispensed at a time.

The potential problem with turning the blocker on and off is that another ball might occupy the space where the blocker is when you turn it on which causes interesting behaviour (usually physics objects going everywhere).

We have a tutorial covering triggers and collision callbacks here which should help:

Hi Steven,

Thanks for the reply!

I tried using a rectangle as some sort of dispenser method for when the ball drops, but then this became problematic when I have 59 balls bouncing everywhere!

The blocker in this case would only be a .1 thickness circle, so if there is problems with physics well then that’s just another adventure to be had!

I had a look at the tutorial you sent me, and will definitely experiment with triggers unfortunately, since I am a newbie when it comes to code, a lot of it is just words to me! But I should definitely have a crack at it!

Originally, I want to set the value of the blocker x = 1
and the values of the balls each have a value of y = 1

If 6 balls past that circle, then the enable = true will happen!

Couldn’t find a tutorial on how to assign values to entities, but will keep looking and experimenting! If you got any ideas, then awesome! :slight_smile:

Many thanks,
Richard Curtis

The easiest would be to create a script with a number attribute. You can either assign them in the editor if all the balls are created in the editor or dynamically if the balls are created in code.

All the balls are created in editor (59 of em), I read the Script Attributes (Thanks for the link!) and what I got was:

MyScript.attributes.add(‘value’, {
type: ‘number’,
enum: [
{ ‘valueOne’: 1 },
{ ‘valueTwo’: 2 },
{ ‘valueThree’: 3 }

So I assume i’d write out to “valueFiftyNine” for all the 59 balls

Unless I created one script for each ball, since each ball is labelled 1-59, the ball that has the number 20 on it will have ‘valueTwenty’ on it.

So at least my next step would be that, and probably assignment all an X number so that I can create some sort of collision gates!

Sounds like a plan!

Many thanks Steven, will keep you posted!
Richard Curtis

You don’t have to enumerate all the values, you can simply have:

MyScript.attributes.add('ballId', {type: 'number', default: 0});

And it will give you a field to enter a number value in :slight_smile:

Ahh okay! So, ill attach a script with

MyScript.attributes.add(‘ballId’, {type: ‘number’, default: 0});

and enter the number value in! (That makes my job a hell of a lot easier!)

Will label them as such and will look up the GUI tutorials on PlayCanvas so when the number has gone into a tube, you’ll see the number on the screen! That will be the next step!

Thanks Steven!
Many thank,
Richard Curtis