Issue #1
Unity3D Game Development Tutorials
Sunday 22nd December 2024
Hey everyone! Today I am going to show you how to make a first person character throw physics based snowballs that explode on impact with a particle effect. This tutorial assumes you are familiar with the user interface of Unity3D and have a basic understanding of the C# language.
All of the tutorials on this site are ad free thanks to supporters on Patreon. If you would like to contribute to the creation of future tutorials then click here and found out about the bonuses donators receive.
To begin with, we will import the rigidbody first person controller from Unity3D's standard assets so that we have a character to work with. In the top menu of the user interface, select Assets > Import Package > Characters. A window should appear with a list of assets. Leave everything ticked and press the import button in the bottom right of the window.
We have our first person controller included in our project now, but before we can add it to the scene we will need to add a floor for it to stand on. Again in the top menu select, GameObject > 3D object > Cube to add a 3D cube to our scene. Now in the Inspector window (located to the right by default) change the position of the cube to X: 0, Y: 0, Z: 0 and it's scale on the X and Z axis to 50.
Now we can add our first person character controller. In the Project window (located at the bottom by default) navigate to Standard Assets > Characters > First Person Character > Prefabs and drag the RigidBodyFPSController prefab into the Hierarchy window (located to the left by default.) Then, making sure the RigidBodyFPSController is selected in the Hierarchy window, go to the Inspector window and change the position to X: 0, Y: 1.5, Z: 0 to position it above the floor we previously added.
Switching back to the Hierarchy window for a second, select the Game Object named, 'Main Camera' and hit the delete key. We do this because the first person controller we added comes with it's own camera.
At this point, if you like, you can test to make sure the first person controller is working by pressing the play button at the top centre of the user interface. Walk around using the WASD keys and look using the mouse. When you are done, hit the escape key to reveal the mouse cursor and press the play button once more to stop testing.
Now we're ready to start coding the snowball. In the Project window, navigate back to the root folder (by clicking on the Assets text in the bar just above the prefab icons) and Right click > Create > C# script. Name the Script, 'ThrowSnowball' and double click it to open it up. It's content should look like the following:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThrowSnowball : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
We are going to add two variables to our new script.
...
public class ThrowSnowball : MonoBehaviour {
public GameObject snowballPrefab;
public GameObject playerCamera;
...
The first variable will store a reference to our Snowball Prefab, which we will use to spawn a copy of when the player clicks the left mouse button. The second will store a reference to the camera belonging to the first person controller, which we will use to calculate the direction to throw the snowball.