Unity 3D - Player Movement and Shooting (Tank Tutorial)
1. In this exercise, we will apply player movement to a vehicle, add materials and a skybox to the environment and enable the player to shoot bullets. 2. Create a new Unity Project and add a new GameObject of a cube. Resize it into a floor 3. Add a cube to the scene to test that the movement script is working 4. Import the PlayerMovement.cs script into your assets folder 5. Drag and drop the script onto the cube then test the game 6. The cube should move forward and backwards and rotate left and right using control keys 7. Delete the cube. We were just testing to make sure the script works. 8. Open the Assets store and search for a vehicle, e.g. Military tank. Download and Import 9. Add the tank prefab to your scene. Add a gameobject-cube and resize it to match the tank 10. Drag the PlayerMovement script onto the cube and test the game. It should move ok 11. Now make the tank prefab a child of the cube. Rename the cube to ‘TankParent’ 12. With the cube selected, in the Inspector panel, uncheck the ‘mesh renderer’ box 13. The parent will become invisible and when you test the game now, the tank should move 14. You can adjust the speed of the tank’s forward movement and rotation in the inspector 15. Next, we will adjust the camera to make the scene more playable. 16. The first method is to make the camera follow the tank’s movement in third person view 17. To do this, adjust the camera’s position behind the tank, then make it a child of the tank 18. Now when you test the game, the camera will always be behind the tank following it 19. Move the camera back out to the main scene by dragging it out into the main hierarchy 20. Import the standard assets package – ‘Cameras’ by clicking file-import package 21. Open the cameras-scripts folder and drag the ‘LookAtTarget’ script onto the camera 22. In the Inspector, drag the TankParent object into the ‘Target’ field of the script 23. Reposition the camera so that it is behind and above the tank at a distance to see all 24. Adjust the x and y rotation to 180 degrees and leave the speed at 1 (lower = faster) 25. Test the scene. When you move the tank around the camera should follow its movement 26. Next we are going to add some materials and a skybox to the environment 27. Go to the assets store and search for a sky box HDR asset. Download and import 28. Click on Window – Lighting – Settings (or Window-Rendering-Lighting Settings) 29. In the Skybox field, select one of the skybox textures you just downloaded 30. Test the game and you should be able to see the skybox applied around the scene 31. Next we will download some materials for the ground texture. Search for ‘tiling materials’ 32. Once you import, you can drag and drop the material onto the ground object to apply 33. To adjust the texture’s tiling, select the material and increase the tiling in the inspector 34. Add a few ramps to the scene by adding cubes, flattening and rotating them 35. Test the game. The tank will drive through the ramps. We need to add some physics 36. Select the TankParent object. In the inspector, click ‘Add Component’. Choose RigidBody 37. Now test the game and the tank should drive up the ramps in a realistic way with gravity 38. Next, we will add some objects to the scene for the tank to interact with and shoot 39. Search the assets store for a tall object, e.g. trashbin, download and import to the scene 40. Add the Trashbin prefab somewhere in front of the tank, resize and rotate as necessary 41. In the inspector panel, with the trashbin selected, click add component and add a ‘box collider’ and a ‘rigidbody’ component. This will allow the player to interact with the object 42. Now we are going to enable the tank to shoot bullets at objects in the scene 43. Import the ‘shooting.cs’ script to the assets folder and drag it onto the TankParent object 44. In the Inspector, notice two fields in the script – one for the bullet and one for the barrel 45. Create a bullet object using a sphere gameobject. Resize and reposition in front of the tank 46. Create a new material in the assets folder and change the colour to black, apply to bullet 47. Finally, we need to add a ‘rigidbody’ component to the bullet in order for it to have physics 48. Create an empty Gameobject and position it right at the end of the tank’s cannon 49. Make sure it is positioned correctly from x, y and z perspectives using the axis gizmo 50. Drag the empty gameobject into the TankParent to make it a child of the tank 51. Select the TankParent object. Drag the bullet into the bullet field of the shooting script 52. Drag the empty game object into the ‘barrel end’ field of the shooting script 53. Increase the shooting speed to 50 and decrease the despawn time to 1 54. Test the game. When you click the left mouse button the tank should shoot bullets Scripts 1. PlayerController.cs - https://tinyurl.com/y2pl5tor 2. Shooting.cs - https://tinyurl.com/yxowakye
Download
0 formatsNo download links available.