In this last lecture for creating a spelling challenge I'm going to add in the functionality that once
you found a word it will move on to give the player another word.
And then once it runs out of words the game over or on is going to come up.
So to begin let's first of all think about how we're going to do this so when do you want to go to the
next word.
Well you could go immediately after you've got three chicks and Clay comes up and does her dance and
you could even wait until Clay has finished her dance by putting some delight in there.
Or you could test when the camera has lost the markers for that word.
And that means that you're ready for another word.
So again do the second option in this case.
And there's no reason why you couldn't kind of modify this for the first one.
So let's go into how spelling control echoed what I'm going to do is add in our last target function
so that we can hook that up to our target case.
Under here under the on image recognize let's add in an own image lost.
And in this method we're going to ask if we've got a reward then pick random word and make sure that
we got reward back to false so that we can get Clay to come up again.
This means that when the image is lost and we've got the reward so Claire has to be doing her dance
we have to have three takes then it's OK to get a new word and sit every word back to false.
So with that let's go back into unity click on your image track to go down to the trackable object.
And now we're going to add in an on image last method so click on.
Plus we're going to drag and drop the spelling controller down into there and then you want to sit its
function which will be the last one we just created.
Now that's going to run when we lose the word.
And only when we lose the whole word Actually it will run every time you lose a single marker but it
will only do its thing when it's lost.
Marcuse that happened to be the whole word.
So let's see what happens when we try this.
So we press play.
We're looking for Cat So that's it cat
OK.
Now we should lose and now we get caught and we happened to be looking at cause that's good.
We'll lose that.
Wants this to do.
Got caught.
Now you can see that the score went up to three now.
So if we lose the word again
which can prove difficult sometimes.
And once again this is the same problem that I spoke about before with doing the random thing that you
can end up wanting to get the random values all the time.
Now it's morning cat and cat
OK so that's it working basically.
So it's doing what it's meant to be doing.
All right so let's stop playing that and to go back into our code what we need to do from here is to
make sure we don't get like five cars in a row obviously because that's sort of not much of a game is
it.
You want to have a whole list of words.
And then once you've got the word right it's ticked off and you move on to the next.
The easiest way to do this would be to just get rid of the wood that we got right from that list so
it can't be picked again.
Now the words are getting picked out of the least in pick random words so just before we do this we're
going to put this code in here.
It's going to remove the last current word that we got because we don't come to pick random word method
unless we've got a word wrong.
And we're coming back in here to get the next word or we're coming in here the first time if we're coming
in here for the first time then current word is going to be minus one up here.
So in that case we will just ignore removing any words.
Otherwise we are just going to remove every one that comes in here that will get it at least.
And therefore this random range cannot pick an index from that list that's going to have any old words
because they're just not there anymore.
So this list is just going to get smaller and smaller and smaller.
Now the problem with that is that you will eventually run out of words and when you do you will get
an index out of range error.
When you try and pick a word that doesn't exist.
So to get past that we need to sort of and the game.
So when we run out of words the game is over bring up that game over.
I can write so I'm going to do that in the on image last section just in here.
So rather than just always do a pick random word we're going up some if statements in here to have if
words don't count the number of words in the least 8 was won.
And we've actually come into here.
Then this is the last word.
So at that point we're going to say that the screen prompt texture the
equals that game over texture that we've passed through else
this here can go up to there.
Now this will bring up our game over.
But the processing will still be going on and you don't necessarily want that to continue because the
game is over.
So when we've brought up the game over at the same time let's go through and make that trackable.
In fact not even the trackable.
What we want to get hold of its parent.
So the trackable trends form the parent game object sit active foal's OK so that is let me just come
in here.
That's this image Cheka.
That is the parent of your trackable So it's just going to turn off that the wiki camera can still be
there but there's no functionality going on whatsoever because the game is now finished.
Right so while we are in here before we test this I just want to mention something else.
If we go up here you probably had this in your mind about the picking of a random word.
What if the words aren't three characters long.
Because remember we set them to be three characters long so back in unity if we have a look at the image
tracker we've got concurrent targets of three.
So four words that are four letters wrong.
I'm not going to work ones that are two letters long will work.
So this kind of has to be set to the maximum.
Can you set this with code.
Currently not.
So you can access the concurrent target's property of the image tracker to read only.
So that does give us a little bit of an issue if you want to have longer words and you want to be able
to change this based on which word you're looking for.
You can't do it.
So you need to sit these to whatever maximum length Your words are.
And if you're creating a simple game like this which is obviously for young children you're not going
to have a really big long words so it should be OK.
Right.
So Becky now code we'll just check that everything is OK.
If you could actually change that concurrent target's length then you would put it down here and you
would set it to the word length.
This one here.
So that length or the length of this which would be the same.
But you know we can't.
So that's just one the mutation.
Right so let's say that and we'll go back to unity and we should be able to play through an entire game.
First word cats
OK and then we look away.
Next word cow conveniently in the right spot look away.
Oh.
And then dog
and we're done and we look away and we've gone to game over.
Aimee's tracker has disappeared.
We've got a score three.
And that's the end.
All right so that's our very simple little spelling challenge augmented reality app and there's so many
different things you could do with this now so you could extend it a lot.
You could add a lot more words you can make a full set of markers for the game.
I actually used to have a more limited version of this and I had my markers and I stuck magnets on the
back and then I could move them around on a magnetic board which made it much easier for a child to
work with.
So that's something else you can do.
You could have your own prompt of text you know show me the word for cat for example so actually sign
it and display an image at the same time.
This just so many different things but this is really a fantastic foundation what you've got now to
build on.
And there's no reason why you couldn't use numbers as well to do math type games and to come up with
answers for that.
Yes so have a bit of fun.
If you do anything with this basic project and expand it for some other idea please share it with everyone.
We love to see your work and I've also got a showcase on my holistic three day Web site where we'd also
love to show off your work to everybody else.
So yeah I hope you enjoyed this little project and have learnt a lot through all the little intricate
bits and pieces that we've put together.