Home || Previous || Next

 

Using the model we imported into Unity – see previous post

 

We now want the character to be an NPC which uses a state machine.

 

Defining the characteristics of the SharkMan:

Screen Shot 2015-11-26 at 11.14.23.png

We then set these values (if you want values to change better to start up high):

Screen Shot 2015-11-26 at 11.21.01.png

If we want hunger to go up if energy goes down (over time he gets more hunger).

Screen Shot 2015-11-26 at 11.16.24.png

We don’t want this to go over 100 as we are using this as a percentage…

Screen Shot 2015-11-26 at 11.21.50.png

The more angry SharkMan gets the hungrier he is. Rage is a given percentage of hunger e.g. you are 50% angry if you are 100% hungry. This means rage will always be 3/4 of hunger.

Screen Shot 2015-11-26 at 11.22.44.png

Remember to call the functions.

Screen Shot 2015-11-26 at 11.19.52.png

Complet Function

Screen Shot 2015-11-26 at 11.23.00.png

Add Shark Stats to Model:

Screen Shot 2015-11-26 at 11.23.37.png

 

We now want Shark Man to patrol, using way point models.

Create 3D Capsule

(the mesh render willed to be turned off later)

Screen Shot 2015-11-26 at 11.25.11.png

Place waypoints and name them

Screen Shot 2015-11-26 at 11.26.57.png

Create new script – Movement

Screen Shot 2015-11-26 at 11.27.48.png

We will need (instantiating) a Navigation Agent – called nAgent, a Vector3 position called Desired position and we need to call a list agent which we waypoints (this  will be using Vector 3)

First call the Collections. Generic in order to add lists to the code

Screen Shot 2015-11-26 at 12.28.38.png

Then institute everything… (mentioned above)

Screen Shot 2015-11-26 at 11.30.19.png

Under the start function we define nAgent and way point

Screen Shot 2015-11-26 at 11.32.29.png

Then we add the waypoints to the waypoint list

Screen Shot 2015-11-26 at 11.33.19.png

Then set the desired posion of way point to 0 as that is the start of the way point list.

Screen Shot 2015-11-26 at 11.34.09.png

We now need to add the counter and the desired Position.

Screen Shot 2015-11-26 at 12.23.07.png

Right a function for the Patrol, in order to do this we need to add a counter and a current state.

Screen Shot 2015-11-26 at 12.07.34.png

Function for Patrol

Screen Shot 2015-11-26 at 12.13.09.png

Particularly long line for the code to move between way points:

if((Mathf.Abs(transform.position.x – waypoints[counter].x) < 1) && (Mathf.Abs(transform.position.z – waypoints[counter].z) < 1)) {

You now need to add a counter and update it when the NPC has moved around all of them  – counting which one it is at.

Screen Shot 2015-11-26 at 12.20.54.png

 

Under Update, set destination to the desired position if you are moving at all.

Screen Shot 2015-11-26 at 11.40.36.png

And also reset the State Machine to the appropriate thing

Screen Shot 2015-11-26 at 12.17.27.png

 

State Machine

If the state is patrolling, run patrol algorithm, and change the state.

Screen Shot 2015-11-26 at 11.56.33.png

This will check the state machine which is to be written.

Screen Shot 2015-11-26 at 11.42.01.png

To start just add the state PATROL

Screen Shot 2015-11-26 at 12.32.25.png

 

Putting the code onto the character

Add NavMeshAgent (first you have to add a RigidBody) You might need to change some of the values for the NavMeshAgent radius so that  it fits nicely onto the character.

Screen Shot 2015-11-26 at 11.46.51.png

In order for the NavMeshAgent to work you need to go to the Navigation menu for the plane.

Set the navigation to static.

Screen Shot 2015-11-26 at 11.48.17.png

Then Bake

Screen Shot 2015-11-26 at 11.48.27.png

The waypoints need to be within the blue after Bake.

Screen Shot 2015-11-26 at 11.49.09.png

 

Add script to character.

 

 

Animation Script for animation to change on different states.

Screen Shot 2015-11-26 at 14.18.20.png

Home || Previous || Next

 

Advertisements