In this lecture we're going to add a little bit of interactivity to our experience so that we can drive
a dinosaur around off its matka and these controls will work on your mobile device as well as in the
editor to begin you'll need a new unity project open up and make sure that you've brought in the we
could choose package and that will bring in the streaming assets as well for this I'm going to use the
streaming assets of dinosaur's which comes with pictures.
We're also going to use one or we could choose models though I have made a few tweaks to the animation
controller on that.
So you'll find the package attached to these select G-code dine I bring that in and you get these Daun
I run a pre fab as well as I don't eat animation controller.
OK so with those things what we got to do is do the same process that we've done before.
So first of all we need.
We could choose a camera and we also need our image tracker to put that in there now the image Cheka.
Let's just take a look at the resource for it or the target collector.
You can use your own Mocca but I'm using the daurna so one and the daughter so one that comes with we
could choose has multiple Machias in it.
And we will get to multiple because a little lighter.
But let me show you how to just sort of target one Mocca if you have multiple in the one WTC fall.
So first of all we'll select dinosaurs and then if you have a look at your trackable you'll say that
there's four different types of dinosaurs in here that we'll be looking for.
Now we're going to use the blue dourness So which is the one at the top so I'm just going to turn all
these other ones off from being active.
So we don't use it.
But if you've got multiple markers in the one WTC fall this is what you will see all the different previews
down here.
Now to this trackable durable object.
We want to put in is out Dinosaur prefab which is this dawn I run.
So drag and drop dash across into the drawable.
All right so now we're ready to try this out.
Actually we need to delete that mine camera first to get a printable marker for this particular dinosaur.
Try NWTS savefile.
There's one inside of the we choose folder.
If you've downloaded all of the examples I haven't done that in this case.
But inside of the examples you'll find another folder called Target images and you'll find a Pangle
that blue Dawna's so I've also attached that particular image as a resource to this lecture if you're
not using those examples before because you might have found.
As you keep bringing in the cat food package and importing everything those samples are really huge
and when you try and swap between android or I'll whistle pay C and has to do Elize rebuilding and it
takes just for ever.
So I find it just easier not to have all those examples if I don't need them.
So we're all ready now to try this out.
Let's press play.
And we grab hold of Amaka which looks like this little fella here.
And you can see a dinosaur.
So this is what he looks like.
He's in an idle animation.
He's also got running and jumping as well as eating attached to him.
And we'll get around to implementing all those in just a moment.
But first we want him to move and we want him to move with some kind of dumb stick control because we're
on a mobile device so we need to put something on the screen that we can move around with the whole
thing is by doing touch controls and these controls will work exactly the same way in the editor which
makes it really easy for us to test.
So let's get about doing that.
So we start by creating a new C-Sharp script let's call it drive.
And inside of that script I'm going to use something that you'll be quite familiar with if you've watched
much else before.
And let me just play this in.
This is code that comes from the Unity reference manual for the entry on input get access and it shows
you how to move an object around in the game world using the arcade or the w s DKs.
So it first of all sets a spade.
That's how fast you're going to move forward and back and a rotation.
That's how fast you're going to spin on the spot.
These are both public and you can then for sit them in Inspectah.
I actually don't like to do that so I remove the public and then just edit it in the code.
But you know it's up to you how you do that.
The translation in this case is using the vertical axis the vertical axis is you up and down arrow case
or the w and s case which will give you a value between minus 1 and 1 and we multiply that by the spade.
So this is you know scaling value for the translation.
The same for the rotation which is the horizontal axis which will bay you will side our case or the
I and the DK again values between minus 1 and 1.
Multiplying that up we then take both of those values and multiply them by time delta time to smooth
them down and make them more even for each frame that gets run.
And with those values we use translation for the Zed movement on the object and rotation for the Y axis
of the object all the up facing axis.
So that is add draw screwed side that switch back to unity.
And what we're going to do is find an asshole prefab and drag and drop drive onto each.
So at this point we haven't included any mobile specific stuff.
We're just going to see if we can move our dinosaur around the Arab case in the editor.
So I press play and then try and find your door.
So all Kemmerer found it immediately.
That's great.
Now I'm using the Eric case and you can see that the dinosaur is moving and we don't yet have animations
that go with that.
But let's go step by step right so this owl dinosaur movement now that spade and that rotation is probably
a little bit.
So we can go back and added that and I'm going to change the speed to one which might seem a bit slower
and the rotation to 50 because these work really well with the animations that we're about to add.
So first of all to use the animations I can't cover everything about the animation system in unity at
this point because it's quite extensive.
So this is a good time to sort of plug my animation course that you can have a look at if you want to
know more about controlling animation and I also have some good animation tutorials on youtube that
you can look up.
So for holistic three day on YouTube first of all to control animations you need to grab hold of the
animator that is on your character.
I'm going to call these.
And just very quickly if I just drag this out and find that dinosaur click on the dinosaur prefab and
have a look over in the right to you'll see that.
Anna Majda is attached to it.
So this is the thing that's holding all of the animations and the transitions between those animations
together.
So we're basically tapping into this.
OK.
Now we've got the animator.
Next we need to grab hold of it.
So after this in the stop function we will sit that UNAM property there to the animated that is attached
to our dinosaur.
Now to control those animations what we do is use trigger values which can be floats or can be boolean
values and that allows you to turn certain animations on and off.
Let's do all of that after we have done our rotate a case in here we've added this bit of code what
it's going to do is check the value of translation.
So translation remember is the value going forward and back.
If translation is greater than zero then we're moving in a forward direction.
If translation is less than zero then we're going backwards otherwise.
Translation must be zero.
And we're not actually moving anyway.
So if we're moving forward we're going to set a bhool inside the animator and it's cold is running we're
going to set it to True and this will turn on the running animation.
Now if we're moving backwards we want to turn on the running animation as well so we're setting that
to true as well if we're not moving.
We want to sit the running animation to false so that we stop running.
Now the difference between going forward and going backward is going to be the speed at which the animation
runs.
So we can run the animation in a Ford direction or reverse direction by multiplying it by 1 or minus
1 which is what I'm doing on these lines here.
So we're setting a float of speed to 1 to go forward and we're setting the flow of speed to minus 1
which will run the animation of running in a reverse format so it looks like it's going backwards.
Save that switch back to unity and we'll run it on the Dinosaur again maybe my camera will find it immediately.
It's in a good spot there.
And so now we can use the UK to move forward.
S-K to move back and the I and the dak to turn around and it also works with the barricades.
OK.
So now we have our animation all sorted.
We're ready to put our mobile controller in adding the thumb stick controller with Unity staed assets
is really really easy.
You want to import them so we're going to go up to assets import package and you want to cross platform
import.
Click on that and then bring that all in and just import it.
Now if you don't have that under your import options then you can go to the asset store which will show
you in a moment when this finishes loading
and in the asset store you'll search for stented assets and it's free you can bring that in.
Now you don't want everything that's going to bring everything and you just want to pull out the cross-platform
input bits because that's what we're going to use.
Ok so I've got them in mind now you'll get a standard assets fold inside of assets and we're going to
go digging around in here looking for cross platform import and then in-a is a prefabs folder inside
the prefabs folder you're looking for the most ball single stick which is this one here.
So this mobile single stick controller we're going to drag and drop that onto the hierarchy.
Now to fully get this functioning we're going to have to go into and build settings and make sure that
we're building for a mobile platform.
So I'm going to switch over to Android and switch platform.
So this will take a little while again.
And with that done you would have seen this button pop it up shut that down.
Now you've got this thumb stick controller and you've got a jump button.
What we also need to do however is make these scaleable so select the mobile singlestick control.
Let's go add component and we want to add how can this gala select that and then for the EWI scale mode
you want to put skyll with screen size.
Now the reason we've just done that is so that when you build this out to whatever device.
Gosh.
And if you've got some of the lightest devices with really really high resolution if you don't scale
it up you'll get these tiny tiny little button on the screen.
You can hardly get your finger on this canvas Skyla.
Just make sure that this will resize no matter what size the screen so see how I'm changing that.
It's not changing its size.
So that's perfect.
Let's just press play and have a look at how it behaves.
So this thums to control a will move you can move it around the mouse.
Now it has nothing to do with the dinosaur as of yet so it's not set up to control those good axes or
at least not in the way that we've been using them.
This is a jump button and we can click on that but doesn't change color whatsoever.
The first thing you might think is I don't like this design and that's fine you can change it.
I particularly don't like this what jumped button you get by default either.
If you want to change the images that are on any of these options under the mobo singlestick control
you can find your mobile joystick and your jump button if you click on the mobile joystick you'll see
it pop up in the inspector.
And on that object is an image.
And this is where you can add your source image in.
So you can change that to whatever you like.
The jump button itself if we have a look at how it's constructed it has text over the top which is a
child of the button and it says jump which is logical.
And in this part you can actually change the different fonts and the colors of the phones.
Size of the phones and that type of thing.
The jump button itself also has an image on it.
If you're not familiar with the graphical user interface elements of unity all of them the ones that
you can see on the screen will have an image attached.
Some way in the hierarchy and that's how you can change them.
We can also change the color.
He so let's just change that jump button let's say let's make it a blue color quite easily just by changing
that and not even playing around with whatever that particular source emerges.
So that's just a very handy thing to know.
Now what happens when you move these objects around or click on these objects is it it's sending information
or commands to the app that is very similar to the keypresses as far as the Ericka's ws de spacebar
and that type of thing.
But what the crossplatform input package does is it takes care of all the different types of platforms
that they might be in Cape's all of the controls very consistent.
So you can use this to inject the I guess the simulation of key presses and that type of thing.
And on a mobile devices it's exactly what you need.
So what we're going to do now is just modify our drive script to pick up those injected events that
are happening so just open up the drive that you've currently got.
We're going to go up to the top and the first thing we want to add in is to use that particular package
which is unity standard assets and then cross platform import.
Now the other change you need to make is to this import and this import.
So instead of using the standard import we simply replace import with cross platform import many JEA
and the rotation one as well with cross platform input manager.
While we're.
Anyway we Maoz will add the code for the button because we didn't do that underneath the very last thing
we did which was to stop running.
We'll just add another cross platform import manage to get button down method and we're gonna get the
jump button the jump button is usually linked to the spacebar.
But in this case it's also linked to that button that is on the screen that blue button that was white
when it's clicked on it's going to set the trigger jump.
Now this jump trigger you may have seen back in the animator window because it was set in there as well.
Right.
So we've now got this getting its input commands instead of from the keyboard from our mobile control
so safe that.
And let's go back to unity and press play.
And once our camera finds a dinosaur we'll be able to move it around with these controls.
OK so what just have to move by hand a little bit to pick it up.
So he's a dinosaur.
And now we can use our controls to move him around like that.
And we have the added feature of a jump animation in there as well.
One last thing we should add just to make him look really embedded in the environment.
Ah of course chateaus so to do that same process before we go to our trackable we right click through
the object we create a plane let's go into a scene so we can see where it is.
There it is.
And we'll leave it at the default saw as pends on the area you want your dinosaur to be able to cover.
We'll also need to put out Transparent shutter Ruseva shader onto it.
So let's just drag and drop that shita in from the projects quickly create a material that zero transmat
material which is going to be set to that transparent shadow Ruseva drag and drop that onto there.
And then we press Play get out Mocca lined and he now has a nice shadow.
Which really does make him look more like he belongs in there and is actually on that pace of pyper
OK.
So that is adding a character and a controller.
Now you don't have to have an animated character you could do this with just a cube if he did do it
with an object that has no animations.
Then let me just go back into just drive code and show you what you would get rid of.
You'd need to just get rid of this part here because this is controlling all of that animation.
This is going to work with all thumb stick and will drive unordinary cube around and you would also
get rid of the animator and the stuff you wouldn't need that either.
OK.
So I hope you enjoyed that and will continue working with the dinosaur and his animations in the next
lecture as well.