Colliding with Obstacles - Unity Game Development Tutorial
You can either watch the video version below or continue reading for written instructions.
Right, let's get started by creating a new 3D project in Unity Hub.
We'll add a Plane for our floor by clicking the plus button in the Hierarchy panel and selecting 3D Object->Plane.
We'll set the Transform of the Plane as follows.
Next, we'll create a material to change the colour of the floor. To do this we'll click on the plus button on the Project panel and select Material. We'll name this material 'Floor'.
We'll then click on the box next to Albedo in the Inspector panel, and choose a green colour.
We can now drag the material on to the Plane to assign it.
We're now going to import some free assets to use in our scene. We'll head over to the Asset Store by selecting Window->Asset Store from the main menu.
We'll then search for 'Low Polygon Soccer Ball', and download and import this free asset.
Then we'll search for 'PBR Cardboard Box', and download and import this free asset.
Now we've got all the assets we need, we'll return to the Scene view.
In the Project panel, we'll navigate to Assets->Soccer Ball->Prefabs, and we'll drag the Soccer Ball into the scene.
We'll set the position of the ball as follows.
Next, we'll add an obstacle for our Soccer Ball to collide with. In the Project panel we'll navigate to Assets->CrowArt - PBR Cardboard Box->Prefabs, and we'll drag 'CarboardBox_1' into the scene.
We’ll set the position of the box as follows.
Next, we'll reposition the camera to get a better view of our scene. We'll select the camera in the Hierarchy, and set the Transform in the Inspector panel as follows.
Let's generate the lighting to improve the appearance of the scene. To do this we'll select Window->Rendering->Lighting Settings from the main menu, and then we'll tick the 'Auto Generate' check box.
Now we're going to look at how we can move the ball by applying a force to it. For us to be able to do this we need to select the ball in the Hierarchy, click 'Add Component' in the Inspector panel, and search for the Rigidbody component.
This will add the ball to Unity's physics system.
Next, we'll write a script to apply a force when we press the arrow keys. To do this we'll click on 'Add Component', search for the script component, and add a new script called 'BallMovement'.
We'll navigate to the Assets folder in the Project panel and double click the script to open it in Visual Studio.
We'll change the script to match the following.
using UnityEngine; public class BallMovement : MonoBehaviour { public float forceSize; private Vector3 forceDirection; private Rigidbody rigidbody; void Start() { rigidbody = GetComponent<Rigidbody>(); } void Update() { float horizontalInput = Input.GetAxis("Horizontal"); float verticalInput = Input.GetAxis("Vertical"); forceDirection = new Vector3(horizontalInput, 0, verticalInput); forceDirection.Normalize(); } private void FixedUpdate() { Vector3 force = forceDirection * forceSize; rigidbody.AddForce(force); } }
We've added a public variable to hold the size of the force we want to apply. Then we've added a private Vector3 variable that is used to keep track of the direction we want the ball to travel.
In order to apply a force we need to access to the Rigidbody component, so we've added another private variable to hold this. In the Start method we're then getting the Rigidbody component and assigning it to this variable.
In our update method, we're getting the input on the horizontal axis and assigning it to a variable. This will give us a value ranging from -1 to 1 depending on whether the player is pressing the left or right keys.
We're then doing the same for the vertical axis.
We're then using these values to create a direction we want the force to be applied in, with the horizontal input for the direction on the X axis and the vertical input for the direction on the Z axis.
Direction vectors should have a magnitude of one, so we are normalizing the vector to ensure this is the case.
The next we're doing is applying the force to the ball. We're doing this in the FixedUpdate method, which is a method specifically for physics calculations.
We're multiplying the force size by the force direction to calculate the force. Then we're adding the force to the Rigidbody.
We'll now save the script and switch back to Unity.
We'll select the ball in the hierarchy, and set the force size to 8 in the Inspector panel.
Let's press the play button to test this out.
If we press the arrow keys, the ball will roll along the plane, and will collide with the box.
Let's press play again to stop the game.
Unity knows how to handle collisions between our objects because they all have collider components.
The ball has a sphere collider component, which allows you to define the centre and the size of the collision sphere.
The cardboard box has a box collider component, which allows you to define the centre and the size of the collision box.
The plane has a mesh collider component, which allows you to select a mesh to be used to detect collisions.
Let's select the cardboard box in the hierarchy and turn off the box collider.
We’ll press play again to see what happens.
The ball will still roll along the plane, but it will pass straight through the box as there is no collider to collide with.
Let’s press play again to stop the game, and we’ll turn the collider back on.
At the moment, the cardboard box is acting as a solid immovable wall for our ball to collide with. To make the box move when it is collided with, we’ll add a Rigidbody component to the box and start the game again.
The box now moves slightly when the ball collides with it.
It only moves a small amount because the ball and the box are configured to have the same mass. Let’s stop the game so we can change this.
In the Rigidbody component for the cardboard box, we’ll reduce the mass to 0.01.
Let’s press play to see how this affects the collisions.
When the ball collides with the box now, the movement is much greater due to the box being much lighter.
We'll stop the game again so we can build a tower of boxes to collide with.
We’ll create a copy of the box by right clicking on it in the Hierarchy and selecting Duplicate, and we'll change the X position of the new cube to -0.65.
Next, we'll hold down the ctrl key and select both boxes in the Hierarchy. We'll then right click and select Duplicate to create two new boxes. We'll set the Y position of these to 0.91.
We'll duplicate the boxes again, and we'll set the Y position to 1.52.
We'll duplicate one more time, and we'll set the Y position to 2.13.
The scene should now resemble the image below.
Let's press play to test this out.
Now when the ball collides with the boxes, the tower collapses and the boxes fall as you would expect.
That covers everything for this tutorial. We hope that you found it useful. Please leave any questions or feedback in the comments below, and don't forget to subscribe to get notified when we publish our next post.
Thanks.
Comments
Post a Comment