Using 3D models in Unity

In the previous lesson, you downloaded a 3D soldier model and imported it into your Unity project, along with a Mixamo animation. You extracted some animations and created an Animation Controller. In this lesson, you'll place the soldier in the scene and test out those animations you set up. Off we go!


Add the Soldier to the Scene

In the Projects area at the bottom of Unity, click inside of the models folder, where the soldier lives. Drag and drop him into your Scene:

Dragging and dropping a 3D model into a scene in Unity

When you play your game and get up close to him, however, you'll see that he's far too small:

3D model at the origial size

It looks like we're about to blast away at a midget! To change the size of your soldier, select him in the Hierarchy. In the Inspector on the right, change the Scale values for X, Y and Z to 1.6:

The scale values in the Inspector

Now when you play your game, he'll look like this:

3D soldier model reccaled

Far more intimidating!

This is the original pose of the model, rather than one of those animation clips we duplicated. If you select your soldier in the Hierarchy and have a look at the Inspector, you'll see that there's a slot for Controller under the Animator component:

The Controller section of the Animator component

Make sure your Soldier is selected in the Hierarchy. Click on your Soldier-Anim folder in the Projects area at the bottom of Unity. Now drag and drop your AC-Soldier controller onto the empty slot:

Dragging and dropping an animator controller onto the Inspector

If you play your game now, you'll see that the soldier now looks like this:

3D soldier model in an Idle pose

This is the Idle state, the one from our Animator. We set the default layer state to Idle, if you remember. In the Animator, right-click on Run. From the menu, select Set as Layer Default State. It should turn orange and have the arrow pointing to it from Entry:

The animator window with the Run state set as default

Play your game again and you should see the player running:

The soldier in the Run animation state

Stop the game. In the Animator, select the Shoot block. Right-click and select Set as Layer Default State. Play your game and you should see the soldier shooting.

Except, he will only shoot once. We need to make him loop. To do that, in the Project area, click on your Shoot animation clip inside of your Soldier-Anim folder. With the Shoot clip selected, have a look at the Inspector. Notice that there is a Loop Time item at the top. Check the box:

The Loop Time indicated for an animation clip

Play your game again and you should see the soldier firing repeatedly.

Now set the Dying block as the default layer state in the Animator. Play your game and watch the player keel over. You may notice a problem, depending on which dying animation you downloaded from Maximo:

A soldier face down demonstrating an issue with the animation

As you can see, the soldier has sunk into the floor! To remedy the problem, select the Dying animation clip in your Soldier-Anim folder. In the Inspector on the right, set the Offset for Root Transform Position (Y) to a value of -0.1:

The Offset for Root Transform Position of an animation clip

Play your game again and you should find that the soldier is not sinking into the floor:

The problem with the animation resoved as all the soldier is now visible

Now that we've tested all the soldier animations, we can do some coding. First, though, click on Idle in the Animator. Set that back to the Default Layer State instead of the Dying one.


Get the Soldier to look at us

If you play your game now, you'll find that you can walk round the soldier and he doesn't notice you. We can fix that with a simple script that will get the soldier to look at the player. (You can also walk through the player, but we'll fix that soon.)

In the Projects area at the bottom of Unity, move inside of your Script folder. Right-click and select Create > C# Script from the menu that appears. Call the Script EnemyLookAt. Double-click the script to open it in your coding editor.

You can delete the Start method as you don't need it. What we do need is to get the Transform of the Player. So, add this line at the top of your code, but inside of the curly brackets of the class:

public Transform player;

It's public because we want this Transform (note the capital letter T) to accept a game object that we can drag from the Hierarchy.

In the Update method, add this line:


This time, transform has a lowercase t. That's because it refers to the transform of the game object you attach this script to, which will be the soldier.

And that's all for this script. It should look like this:

Unity C# code for the LookAt method

Save your work and return to Unity. Drag and drop your EnemyLookAt script onto the Soldier in the Hierarchy. (You can also drag and drop it onto the Inspector when the Soldier item is selected in the Hierarchy.)

With the Soldier still selected, drag the First person controller item from the Hierarchy onto the empty Player slot in the Inspector:

Dragging and dropping a game object onto a script in the Inspector

If you were to play your game now and walk around, you'd notice that the soldier still ignores the player. To remedy that, click inside of your Soldier-Anim folder in the Projects area at the bottom of Unity. Select the Idle animation clip. In the Inspector, locate the Root Transform Rotation item. Change the dropdown for Based Upon (at Start) from Body Orientation to Original:

Changing the Root Transform of an animation clip

Play your game again and you should see the soldier follow your movements.


Walking through the Soldier

You can walk through the soldier, at the moment. To prevent this, just add a box collider to the soldier. First, select the soldier in the Hierarchy. In the Inspector, click the Add Component button at the bottom. Type Box Collider in the search box and then select it. (Don't add the Box Collider 2D by mistake.)

In the Scene, notice that the box collider is too low (it's the green square near his feet):

A Box Collider added to a 3D model

In the Inspector, change the Size values on the Box Collider to these:


X: 0.5
Y: 1.3
Z: 1.4

For the Center, change the Y value to 1.9. The Inspector will look like this:

The Size values for a box collider

And the Scene will look like this:

The box collider repositioned

The green box in the image above is the box collider. This will be where the player's shots will register. If the player hits anywhere within the green box, it will score a hit. Anywhere outside will be a miss. You can play around with the two Y values in the Inspector, if you're not happy with the box collider's position. For us, any leg shots will be a miss. But you might want to change this.

Anyway, test it out. Play your game. You should find that you can't walk through the soldier anymore.

Now let's write a script so that we can shoot at the soldier and have him take damage. When he takes too much damage, we'll kill him off and play that Dying animation we set up. The first step is to fire at the enemy. We'll do that in the next lesson. Bit of coding to do!

<--Back to the Unity 3D Course Contents Page