These tutorials cover some of the most common actions that will be applied to objects in your game. This tutorial is noted from the videos available on Unity’s website.
ACTIVATING GAME OBJECTS
Game objects can be switched on and off by inserting the line below, and placing true or false between the brackets. gameObject.SetActive(false);
This line will deactivate the object the script is attached to, as well as any child objects attached to it. Note that whilst switching child objects on and off is possible, they wont appear in the scene unless their parent is also activated.
To activate a different game object, create a GameObject variable (note the capitalisation on the variable type) and type
variablename.SetActive(false);
TRANSLATE AND ROTATE
These functions are used to change the position of a game object relative to it’s position in the world space of the scene. The simplest way to do this is with a line like:
transform.Translate(new Vector3(0,0,1));
When placed in the update Method which runs every frame, this will move the object one metre in the z direction per frame.
We can build upon this in several useful ways. Rather than using the (new Vector3(x,y,z)) format, we can simplify it down to (Vector3.forward). We can even use (-Vector3.forward) in order to move the object backwards.
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
moveSpeed is simply a float variable which can be used to increase or decrease the final movement speed, and Time.deltaTime is a commonly used function that will change the translate to metres per second rather than metres per frame, which is much more realistic.
Transform.Rotate is similar but with a slight difference – inside the brackets – rather than just including a numerical magnitude value, we must also include the axis around which the object rotates. So the code becomes:
transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
Note the comma to separate the two parts of the argument.
NB: You should not use the translate and rotate arguments with an object that has a collider and rigidbody attached – instead use the forces and physics functions instead.
The only way you should use the transform and translate on a rigidbody object is if it is set to ‘Is Kinematic’ which will disable any physics forces from taking effect on that object.
LOOK AT
Often times we want one object in the scene to be facing another, for example we want the camera to track the player, or have enemies look in his direction. To do this we use the LookAt() function:
transform.LookAt(target);
When applied to the camera for example, this script will make the camera rotate to always have the object in the ‘target’ variable in the centre of the screen. For this to work we must also create a Public Transform target; variable. ‘Public’ means it can be accessed from the unity inspector, and the ‘Transform’ type stores the positional data of the selected gameobject.
INSTANTIATE A GAME OBJECT
Instantiation is used to create clones of an object. In the context of Unity, this will often be a clone of a prefab – A premade object saved in the assets folder which can be added into your scene when needed. An example of instantiation is when a character shoots a rocket out of a rocket launcher. The simplest form of the code is:
Instantiate(rocketPrefab);
Where rocketPrefab is a predetermined Rigidbody variable. This has the problem however of simply creating the new object at the centre of the script object. For the rocket launcher example, we would want the rocket to appear from the barrel, in the direction the launcher is facing. As such we can elaborate on the instantiate command by including position and rotation factors in the argument. For example:
Instantiate(rocketPrefab, barrelEnd.position, barrelEnd.rotation);
Here we would create an empty game object at the barrel of the gun, and save it in a Public Transform barrelEnd; variable. Now the rocket will instantiate in the right place.
MAKING THE ROCKET SHOOT FORWARD.
So we can instantiate a rocket, but having it stay still or drop to the ground is no good. To make it shoot forward we will want to add a force to it. Assuming we want our rocket to react with other gameobjects, i.e. explode on contact, it will contain a rigidbody and a collider component. As such, simply using Transform.translate will not suffice as this does not work with physics objects.
To get this to work, we must alter the code from before and add a few extra bits:
Rigidbody rocketInstance;
rocketInstance = Instantiate(rocketPrefab, barrelEnd.position, barrelEnd.rotation) as Rigidbody;
rocketInstance.AddForce(barrelEnd.forward * 5000);
Instantiate will create objects of default type ‘Object’, so we add the ‘as Rigidbody‘ to the end to make it a rigidbody object, which can be affected by forces. Then we declare a new Rigidbody variable and save our instantiated object into that variable. Finally we add a force to the object held in our variable. Physics forces will be more thoroughly discussed in a later tutorial.
DESTROY A GAME OBJECT
To remove an object from the scene, we can simply include the following line in its code:
Destroy(gameObject);
We can also include a second part to the argument to add a time delay to the destruction. In the following instance, the object will be destroyed after 3 seconds. However, the time can also be set to a Public Float Variable and changed from the inspector:
Destroy(gameObject, 3f);
One issue with using this line of code is that it will also destroy the script when used, so an alternative is to have the script target another object and destroy that instead. To do this, we create a variable called Public GameObject other; assign an object to it in the inspector, then use the code:
Destroy(other);
Again, this can be set to work on a time delay, and can be combined with other statements to happen automatically or upon other behavious such as the press of a button.
DELTATIME
The term ‘delta’ means the difference between two values. The deltaTime property of the Time class is essentially the time between each Update or Fixed Update. This can be used to smooth out movement or other incremental calculations.We saw this earlier with the line:
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
Time.deltaTime will measure distance as metres per second rather than metres per frame. Since a computer will process frames at different speeds depending on how resource intensive they are, it is important to make this distinction.
Comments