In this lecture we're going to add some different functionality to our little dinosaur app instead of
controlling the dinosaur with our mobile thumb stick thing that we've added.
Well going to add the functionality so that we can touch the screen or click it when we're in the editor
and drop a hamburger on to that shadow plane.
So the shadow plane that's sitting here having the shadows cast onto it.
We can also use it to detect touch collisions and then to instantiate something at that location such
as a hamburger that I've got here and Mike a virtual character move to that position.
So it's very much the same mechanic that you've got like a touch to move in many games when you just
click on the environment or the landscape and your army or troops or whatever go off and over to that
location.
So let's stop doing that.
First of all you're going to need something that represents food for me.
I've got this little hamburger model and you can find these in the store.
You can also use a little cube if you like.
So just create a cube and make a pre-fab out of it.
I'm going to use my hamburger.
So I want to remove my hamburger from the hierarchy because I've already got a prefab sitting there
of it.
Now the plane itself which is here that we added this default line without shadow shadow on it it's
going to automatically come with a mesh Collider and it's very important that we have this mesh Kolata
because we want to be able to detect when a touch has hit it.
And we're going to do a physics right cost.
So we're going to click on the screen or touch the screen of your mobile device cost array straight
out in the same direction that the camera is pointing from that position and then determine where it
ends up hitting this plane and we'll grab that point and put a hamburger there.
OK.
So let's create some code to do that.
So right click and your assets and we'll create some script called touch to move.
Now let's open that up to instantiate your food object.
You'll need the script to know about your food objects so let's create a public game object.
Food prefab so we can pass it through in the inspector.
Now what we want to do is sort of out update is in fact taste for touch and that goes like this.
So we can use input don't get mouse button down zero.
This is the left mouse button.
But for a mobile device it's also a single finger touch.
So it makes this code work in the shower.
And also on your mobile device which is really really handy so we can get that same functionality we
can test it without having to build it out a million times.
Now we're going to test if the mouse is down.
This card is testing if there is already food or if there isn't food on the ground.
Then we will make new food.
Let me just get rid of this for now.
And we won't use it in there so we can add multiple food objects on the ground.
The other thing we do need to do at the top here is make another game object called Foo's that's going
to hold on to the last lot of food that was added to the environment.
So what's going on in sort of update.
Well we're creating a right cast hate cold.
This is going to hold all the information about what our right cost hit when we cast it into the environment.
Now to cost a hit or a cost array from your camera from the mouse position into the game world we use
camera and main dot screen point to Ray from the mouse position which is taken from input mask position.
So it projects a line in the forward direction of the camera straight into your game world and that
is then stored as these.
Right at the moment we've just got a ride going off into on varment.
Now if we know anything about race it's basically a vector being projected out into the environment.
Now we then take that Ray and ask it if it's hit any physics object that ground plane is a physics object
because it has a kilometer on it.
So we can detect that with a physics dot right cause we send the right.
We then give the.
This is our request that we credit to that method so that we can return any information about a hit
that occurs then we give it a distance.
So a ride goes off pretty much forever.
You don't want to keep testing for ever and ever and ever.
If you've hit something unity won't cost a ride out to infinity before it gives up.
But you do need to say you know within I'm going a distance of ten thousand which is probably a bit
of an overkill for this application.
We're going to keep testing until we've hit something.
If you have something like 10 head Tanenhaus and you're casting for 10 and you are tapping the screen
and nothing was happening chances are that you're right is just way too short.
Now I've seen this many many times when students put together their projects and they're doing physics
right costs and trying to detect if they've hit something.
And the biggest problem with it not working is that they're right is just not long enough.
I think even 100 is probably OK.
I'ma just go for a thousand.
If this doesn't work we'll know that we should come back and make it a little bit longer.
But the game world itself that we're working in doesn't have that kind of scale so we should be pretty
safe with a thousand after the physics right.
Cause if it does hit something it returns true so then it's going to go down and this next little test
here what we're going to test is if the thing that was hit which is going to be hit Don't Kolata game
object.
If it's tag is equal to ground all in lower case then we will set something called Goal pause to the
hit point position now golfballs is going to be a vector 3 values storing that hit location which will
be a point on that Shadowline we need to declare this at the top.
So up here let's just add in vector 3 goal posts like that so I can stop complaining about it.
Now at that go polls we're going to instantiate our food item which we're then capturing and storing
in the property we set up before instantiate.
From the food prefab at the goal paws we're just going to use as zero rotation.
So Catoni and identity will give you just a zero rotation matrix and then way invoking the remove food
which you get rid of the food.
After 4 seconds we haven't yet written this method so let's now add that in after update let's just
in here insert this remove food it's very very simple.
It's just destroying whatever the food object was.
Now come to think of it I'm going to put that and the dark took up before up here back in.
So I'm going to taste if the food itself cools.
Now that means there's no food in the environment.
Then it will allow for the recast to happen.
Create the food then you won't be able to create any more food in the environment until after four seconds
when it's destroyed.
And this food becomes Now again so let's save that switch back into unity.
What we're going to do now is remove al thumbs to control it because we're not using it.
Or you could just turn it off actually we'll leave it there.
And we want to go to our dinosaur model.
It's prefab and we need to get rid of our drive script again.
You can just turn that off.
It's just not going to run.
We want to add a touch to move to our dinosaur and we want to give it the food prefabs so I'm going
to grab my hamburger and put that over there.
You could use as I said before a cube prefab.
Now if you remember from our code right cost was only going to consider touching something that had
a tag of ground.
That means we need to click on now plying Asherah plane.
Then in the Inspectah for the tag let's just drop that down.
We want to add in a new tag which I will now there and they will call it ground lower case because it's
what we're looking for say That could the plane again and then find ground in that new list and add
it.
OK so now when I hit something and it's tagged with ground that should be the position where we get
our hamburger.
So let's press play Costa Donnison is not going to do anything at the moment.
And remember that the code is on the dinosaur.
Right so it's not going to run unless there's a dinosaur and if you look in your hierarchy there's no
dinosaur and you can see that clearly there's no dinosaur.
But the trackable is not on.
So the camera hasn't detected it let's just find a dinosaur and that will turn the dinosaur on and also
then activate our code.
So now we can click and add a hamburger.
Now if I try and click again while that hamburger is there it won't let me add another one.
But as soon as it disappears I can add another one which is what we want.
We don't want to confuse our Donna.
So we want to walk over and eat the hamburger that is available in only one single hamburger.
OK.
So let's now add that functionality into the script.
To begin with we'll go up to the top and we're going to add some new variables up here just like we
had before when we were moving the character around manually.
It needs a spade and a rotation spade and you can see about it spade here and it's at point five and
the rotation spade is at two.
Now you can modify these based on again how the animations actually work with the character as it's
moving around the screen.
If you find that it looks like it's slipping on the ground.
Chances are that the animation is running too fast for the spade the tool trying to move it.
So you will need to adjust the speed.
We also need an accuracy value.
We're going to ask the dinosaur to move towards a certain location.
So the dinosaur could be anywhere in the world and it has to travel to a particular goal location.
Now we're working with float values which are quite accurate really.
The saw the chances of the Dawna's are actually getting right on that perfect goal location a very very
small.
We want to sort of say when you are close enough that is good enough.
So the accuracy in this case we're saying if you're within 0.5 of your goal location then consider yourself
being at your goal location and you can modify that again based on how close you want the dinosaur to
get to its goal.
If you find that the dinosaur is going towards its goal and then ends up going ran and in circles near
the goal your accuracy is too small and therefore you will need to make it a little bit bigger.
Now the animation in this case is going to be controlled by our annum.
That means we need to have our anthem set in the spotlight before so it's a the start get component
and to.
And then we can program those animations in with the movement.
So now let's go down to the bottom of the code.
We're going to put in a light update this time.
OK so let's go through this light update the light up day is going to determine the direction that the
dinosaur needs to look.
So we have this look at goal.
It's going to be the point in the environment we want the dinosaur to look towards.
Now in this case we've got a dinosaur model and we have a hamburger model and they're both on a plane.
We can't be sure that their origins are in exactly the same position.
We only want to turn a dinosaur in the X Zed.
So we want to turn it around it's why we don't want to tip the dinosaur up and down.
So that would be modifying its pitch and to do that we then use only the x and z values of the hamburger.
But we use the dinosaurs own y position which will make sure that it can't possibly tip over the wrong
way.
We then work out the direction that the dinosaur has to turn to look at the goal.
And we use that direction to then perform a slip.
So this slip is going to slowly turn the dinosaur towards the goal location.
So it works out according to the rotation spade little bit by a little bit to turn around.
H light out.
All of this code here you could replace with a these dot transform dot.
Look at goal poles that would snap the dinosaur around to look directly at the hamburger rather than
slowly turn.
Now this code is very commonly used in AI algorithms that move characters around in a game environment.
Following this is where that accuracy comes in.
We're going to test if the position of the dinosaur to the goal that it's looking at is within the accuracy.
And if it is then or in this case if it's greater than accuracy then you haven't got to the goal yet.
We're going to switch the running animation to true and we're going to perform a translite which will
push the dinosaur forward so this is doing very much like the Arrow case we're doing previously in fact
is almost exactly the same except that you've got a speed value putting in rather than what the Ericka's
are saying.
We're also setting the animation speed to 1 because remember previously we were setting speed to one
if we wanted to go forward and minus one if you want to go backwards.
In this case the dinosaur isn't going backwards whatsoever it's always going to go in a forward direction.
But that speed if you set it to one is going to still be set to zero in the animation and you won't
get any animation running whatsoever.
Now if we with any accuracy which is in this Else part then we're going to stop running if we need food.
OK so with the third object is not equal to.
Now we will then start aging it.
So he say it is ading to turn.
And if you have a look in the animator for the Joana So you'll see that there is a Boole and they're
called is aging now after that if else statement we then have another one here to say if the food has
disappeared.
If food is equal to now then sit your eating to false so stop ageing and you'll see this effect as we
play it.
OK so we've got that say that.
Let's go back into unity and we'll run it and have a look at how that goes down.
Now let's get our Mokka
So we have a dinosaur and now we can click on a hamburger and he'll walk over have a bit of a munch
and it will disappear and I remember that the aging animation doesn't have anything to do with the hamburger
disappearing that hamburger is still on a four second Sokal.
So the further away you put the hamburger you can see in that case the quicker it was for it to eight.
So far just put it right there.
He will eat it for a while.
And then it will disappear and then put it right over here it'll take a while to get to it.
Meanwhile those invokes a counting down to number of seconds of four which is making the hamburger just
disappear.
OK so there we go.
Now we have the ability to click to move again character and Amite that game character and also add
other objects into the augmented scene which aren't related to any Marka.
All of this code will also work on mobile devices which is what's really Noss about that.
Get mass down of the import class that it will work right across the platforms.