tiistai 14. helmikuuta 2017

UNITY FPS - Enemy

NAV16AP

3. jakso lukuvuonna 2016-2017

Now that you have done player movement and shooting it’s time to create some enemies.

Before creating the enemy, make some walls in your scene with different sized 3D cubes.

For this tutorial you’re going to need Playmaker ecosystem package. If you don’t have it already download it. Open the ecosystem window and type in the search pathfinding. Download and import the package.


Make every wall and floor in your scene static by selecting that object and pressing the static button. Open the navigation tool from the top tool bar under window. Then go to navigation and press bake.

Create new 3D capsule and insert new fsm for it. This fsm will work as ai for the enemy. Also make a new tag called enemy and tag your new capsule with it.


In the new fsm the first state has action find game object.


Create new game object variable and name it as Player object. Choose player tag as the tag and your game object variable in store section. This state identifies and stores the player object that can be used in later states.


Next state will have 5 actions: smooth look at, raycast, set agent destination as game object, set bool value and game object compare tag. For this, create a new game object variable called test, game object variable called empty and bool variable called insight.

Smooth look at should have your player game object variable as the target object and make finish distance 0 so the enemy will always look at the player.


Raycast on z axis and choose in the hit object section your test object.


Set agent destination as game object has you empty game object in the destination. Game object compare tag compares your player test object to the player tag. Create event called move and select it as the true event. Lastly create action set bool value and select insight as the bool variable.


Next state has also same 5 actions. This time use your player object as the destination in set agent destination as game object. Game object compare tag will have finished in the false event and insight bool value will be true.



Enemy will need a health pool so that player can damage it. Create new fsm for the enemy.
The fsm will need float variable called health and one event called death and second event that is global called hit.


In first state you need to set the health at 10. This is the max health the enemy has.


Next state will have action float compare. Choose float as your health variable and for the equal and less than sections make it send the death event, which will transition to a third state.


Third state will have destroy self action.


You also need fourth state which has the hit as global transition. In that state use float subtract action and choose 1 as the amount to subtract.


Lastly make some changes to the bullet movement fsm. Start by creating 3 new events: hit, hit enemy and not enemy. Also create new game object type variable named Enemy. 



In the first state add raycast action. Select the z axis to be the axis where raycast happens and make the distance 0.9. Select hit in the hit event and your enemy variable as the store hit object. Make a new transition named hit from the first state to a new state.


Third state will have game object compare tag action. Choose hit enemy and not enemy as transitions in this state and link those to the true and false event sections. Select your enemy variable as the game object and enemy as tag.



Fourth state has send event by name action. Select game object fsm and specify game object with your enemy object as the specified game object. Choose right fsm of the enemy and send hit global event.

Ei kommentteja:

Lähetä kommentti