Basic Interaction System for First Person Character


Here is a solu­tion for a First Per­son Play­er to acti­vate but­tons, switch­es, levers and more in Unity.

The solu­tion con­sists of three Scripts that inter­act with each oth­er to solve the detec­tion of the inter­ac­tive ele­ments and their acti­va­tion. In addi­tion, two Scripts are pro­vid­ed as exam­ples to achieve what is observed in the fol­low­ing video on the left:

This is what we want to achieve


In this video we see how this basic inter­ac­tion sys­tem for Uni­ty works and we also down­load the Uni­ty Pack­age from this arti­cle and con­fig­ure the inter­ac­tion sys­tem in Unity.


It is impor­tant to men­tion that in this solu­tion no par­tic­u­lar action is solved, although two Scripts are pro­vid­ed that exem­pli­fy the use, each par­tic­u­lar action must be programmed.

Description of the download files

This solu­tion con­sists of three basic scripts: "Check­In­ter­ac­tion", "Inter­ac­tion­Re­ceiv­er" and "IAc­tion

Basic Inter­ac­tion Sys­tem for First Per­son Character
Ver­sion: 1.1
Pub­lished: April 3, 2020

Check­In­ter­ac­tion is in charge of check­ing if you are observ­ing some­thing that you can inter­act with, you can add it to any GameOb­ject but it is coher­ent to place it in our character.

Inter­ac­tion­Re­ceiv­er is placed on all objects in our world that the char­ac­ter can oper­ate, for exam­ple but­tons, levers, doors, etc. The objects assigned to this script must also have a Col­lid­er assigned to them so that the Check-Inter­ac­tion Script can detect them, oth­er­wise you won't be able to add this Script to the GameObject.

IAc­tion is a pro­gram­ming inter­face, it is not applied on any GameOb­ject but it is nec­es­sary for the solu­tion and we must pay atten­tion to how to use it.

In addi­tion, two more scripts are pro­vid­ed as exam­ples to under­stand how the solu­tion is used.

Extra: User Interface

The pack­age also includes the sim­ple user inter­face seen in the video, which allows you to dis­play mes­sages on the screen.


Application Example — Activate Button in Unity

Now let's see how to use this solu­tion to be able to acti­vate a but­ton in Uni­ty and per­form actions with that.

To begin with, we start with a sim­ple scene where there is a but­ton and a gate.


We placed in the scene the FPSCon­troller pre­fab from Stan­dard Assets, if you do not have the pack­age you can down­load it by click­ing here.

We assign the Check­In­ter­ac­tion Script to this pre­fab, the field "min­In­ter­ac­tionDis­tance" is the min­i­mum dis­tance the play­er has to be in order to inter­act with the object, in my case I put 4. 

The "ray­O­ri­gin" field is the point from where the ray orig­i­nates to check the inter­ac­tion, in this case I place the character's cam­era, that way the char­ac­ter will be able to "see" the objects that are in the cen­ter of the screen.

first person control inspector in unity, script to check interaction and activate buttons or switches in unity
Fig. 1: The Check­In­ter­ac­tion Script is assigned to the first-per­son controller.

Receivers — Buttons or switches

The objects that can be oper­at­ed (such as but­tons, switch­es, levers, etc.) will be assigned the "Receiver­In­ter­ac­tion" script, this will make the "Check­In­ter­ac­tion" script detect these objects and allow them to be oper­at­ed with the E key.

We can add a mes­sage to the inter­ac­tion receiv­er and then do things with this mes­sage, for exam­ple show it on the screen as seen in the video, for this there is a region marked with com­ments in the Check­In­ter­ac­tion Script in which a Debug.Log is done show­ing the mes­sage in the console.

buttons and switches in unity, how to interact with objects in unity
Fig. 2: The Inter­ac­tion­Re­ceiv­er Script is assigned to the object with which you can inter­act. This object is also assigned the Script switch of the example.

Objects with action

When we press the but­ton two things will hap­pen, the first is that the gate will open or close depend­ing on its cur­rent sta­tus, the sec­ond action is that the but­ton changes to green when the gate is open and to red when it is closed.

These two actions are pro­grammed indi­vid­u­al­ly using two Scripts: "Switch" assigned to the GameOb­ject of the but­ton (fig­ure 2) and Gate (Por­ton in span­ish) assigned to the GameOb­ject of the gate (fig­ure 3).

inspector of a gate that is operated in unity
Fig. 3: The gate is assigned the Gate Script that comes in the down­loads as an example.

Scripts that solve the actions

The actions must be specif­i­cal­ly pro­grammed, that is, we must cre­ate more scripts that solve the action. This will depend on the needs of each one.

In this arti­cle we are going to see the two scripts that solve the but­ton and gate actions.

The require­ment for action scripts is that they imple­ment "IAc­tion", a pro­gram­ming inter­face. This will require our script to have a method called "Acti­vate" defined, with­in which we will do every­thing the action needs to do when it is acti­vat­ed by the receiver.

In fig­ures 4 and 5 we see exam­ples for the but­ton and for the gate. I'll prob­a­bly make videos explain­ing how to solve more mech­a­nisms using this basic inter­ac­tion sys­tem and prob­a­bly we'll improve it as well.

script for the behavior of a switch
Fig. 4: The exam­ple script "Switch" imple­ments the Iac­tion interface.

unity script for the behavior of a remotely operated gate
Fig. 5: The exam­ple Script "Gate" imple­ments the Iac­tion interface.

Assigning Actions to the Interaction Receiver

Once we have the actions pro­grammed, we assign them to the cor­re­spond­ing GameOb­jects and select the object we use as the trig­ger for those actions (in this case the button).

In the Script Inter­ac­tion­Re­ceiv­er we have a field called "ObjectsWith­Ac­tions", which is a vec­tor of GameOb­jects in which we will place all the objects that will be acti­vat­ed when we press the button.

The objects that we place here must have an assigned Script that imple­ments IAc­tion to work, in the val­ue "Size" I put 2 and assign the GameOb­jects Gate and But­ton, which have assigned the Script Gate and Switch respec­tive­ly (see fig­ures 3 and 6).

Fig. 6: In the Inter­ac­tion Receiv­er we con­fig­ure the actions.

With this we achieve that when press­ing the but­ton the Inter­ac­tion­Re­ceiv­er Script takes the objects "Gate" and "But­ton", get the Scripts that imple­ment the IAc­tion inter­face and exe­cute the Acti­vate method of each one.


In this arti­cle we have seen how the basic inter­ac­tion sys­tem works, with this solu­tion we'll be able to place inter­actable objects in the scene (like but­tons, switch­es or levers) and when acti­vate them one or more actions will be performed.

Any action we need to per­form must be solved in par­tic­u­lar using a Script, the only require­ment is that this script imple­ments the IAc­tion inter­face and there­fore with­in this script a pub­lic method called Acti­vate must be defined.

Scroll to Top
Secured By miniOrange