Object Interaction in Unity3D
In a video game interacting with surroundings what makes the game enjoyable and realistic. In this post, I will show how to simply interact with objects in Unity3D. Let’s get started!
Initially, I have a scene including two rooms that are connected to each other with a short hallway. There is a door in the room where my First Person Controller stands in.
Our purpose is to enable player to open and close the door with mouse input.
Add a C# script to your project ( Assets > Create > C# Script ), rename script file as “DoorInteraction”. Open your script file, so we can start coding.
Check your class name! The class name and script file name must be the same to enable the script component to be attached to a GameObject.
By default you have your Start() and Update() functions. Let’s add some event functions such as OnMouseEnter(), OnMouseExit() and OnMouseDown(). These functions help us to interact with our door object.
Instead of OnMouseDown(), you can also use OnMouseUp(). Difference is, OnMouseUp() function is called when the mouse button is released and even if the mouse is not over the same object.
You can throw some debug statements to test your mouse events. If you see the proper output on the console you can continue, but if your mouse events don’t work check whether your object has a Collider.
Mouse events only respond to a Collider or a GUIElement.
In order to keep track of the state of the door, create an enum called “DoorState”. Define a variable from DoorState and initialize in Start() function.
When we click the Door object, it must act properly according to its state. To do that update OnMouseDown() function:
In OpenDoor() and CloseDoor() functions I will use animations to move the door. I have two animations called “DoorOpen” and “DoorClose”. These animations only moves the door horizontally.
You can also move the door object by changing its transform. ( I prefered animations because my FPS count was lower using animations. )
If you are using animations don’t forget to uncheck “Play Automatically” property:
Write OpenDoor() and CloseDoor() functions:
Now, you should be able to open and close the door when you click. If you haven’t noticed yet, we have a small problem that occurs when you click the door repeatedly.
While the door is opening, if you click it, the door will start closing without finishing the opening action. We need some kind of delay to wait for the animation to end.
Coroutines is a good option to add delays. A Coroutine is like a function that is executed in intervals. These functions work with special yield statements to return the code execution out of the function. Then, when the function continues, execution begins again where it went from left on.
If you don’t have animation you can define a variable to how long you want delay to be.
Update the OpenDoor() and CloseDoor(), change their return type to IEnumerator because it allows to use yield WaitforSeconds() function:
Also, update the OnMouseDown() function, we must call OpenDoor() and CloseDoor() using StartCoroutine():
You can see your door’s state in the Unity’s inspector tab since we made it public:
Additionally, add some sound effects. Attach an AudioSource component (Component > Audio > Audio Source) to your Door object. Drag and drop your audio file to the Audio Clip field:
Create a variable for playing audio file:
Get back in the Unity and you’ll see the variable the “doorSound” in the door object’s inspector tab. Assign your audio file to that field, by drag and drop.
Add statements to play audio in OpenDoor() and CloseDoor() :
Optionally, we can add some visual indication that, you can interact with the object. Simply, we can change the color/material of the object. Create a variable to store the default appearance of the object and initliaze in Start():
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class DoorInteraction : MonoBehaviour {
public enum DoorState { open, close, inProcess };
public DoorState state;
public AudioClip doorSound;
//Define a variable to store the default color of the object
private Color defaultColor;
void Start () {
state = DoorState.close;
//Get object's "Color" property
defaultColor = renderer.material.GetColor("_Color");
}
We want to change the appearance of the object when mouse is over on it. Change back to its default appearance when mouse exits from the object:
Now, let us write the changeApperance:
You can download the script file here: