maanantai 13. helmikuuta 2017

UNITY FPS - Shooting

NAV16AP

3. jakso lukuvuonna 2016-2017

Now that you have done character and camera movement it’s time to move on.

To be able to shoot you have to make a gun for the character. Create an empty game object and move it to the characters side in a position that a gun would be in. Then create a cube that is child of the gun position object. 


Scale the cube to rectangular shape and move it forward so that it shows in the game camera at bottom right corner. Remember to deactivate the box collider for the gun.


You’ll need one more empty game object that is the bullet spawn point. It should be located right in front of the gun barrel.

Create a 3D sphere, which will be in the bullet spawn position. Add FSM to the bullet.


The first state has translate action. On the z axis choose a number that is your bullet speed. Add action wait  and transition event finished. 


The next state has one action called destroy self, which will destroy the bullet.


Create a new prefab in the project folder by right clicking it and choosing prefab. Name the new prefab as bullet and drag your bullet from the scene hierarchy to the prefab. You can now delete the bullet from the hierarchy.


Next add FSM to the gun holder object. Create 2 events called fire and fire again. Also create two new states.


In the first state use action get button down. Button name will be Fire1 and it uses the left mouse button. Select fire in the send event section and make transition event fire to go to the second state.


In the second state you’ll have three actions wait and get button up. Use the fire again transition event in the wait action and choose short length of 0.03 for it. Make the transition to the third state. For the button up use finished transition event and connect it to the first state. Third action will be create object. Select your bullet prefab as the object and drag your bullet spawn to the spawn point.



Third state has wait and get button actions like in the second state. Make finished transition to the first state and fire again event to the second state.

Ei kommentteja:

Lähetä kommentti