Time for action – scripting the collectible
The first thing we'll script is some basic movement. Typically, the collectibles in games have some basic animation to differentiate them from the static objects in the scene, and it helps the player identify where they are. For our coin, just a constant rotation should suffice to make it stick out. Perform the following steps to script the collectible:
Right-click on your
Scripts
folder in the Project window and create a new C# script namedCoinRotation.cs
.Double-click on the script to open it in your code editor. Add the following lines to the script's
Update
function:void Update() { gameObject.transform.Rotate(0, 0, 5); }
These lines access whatever the
GameObject
script is attached to and call thetransform
property'sRotate
function, which takes three parameters (one for each axis of rotation). We want to rotate our coin along the z axis, so we added a value of5
in the Z axis parameter field and left the other values as0
.Tip
The
transform...