6: Adding Interaction to the box

Step 1:
We now need to set up a new input in the input Manager. Go to Edit > Project Settings > Input and you should see a size element add one to the current number to add a new Input. In this example we have changed the value from 18 to 19.
quest020
This will create a duplicate of the final input on the list which we can now edit.

Step 2:
We can now set up the input called Interact. This example is set up for the B button on an XBOX Controller for both Windows and Mac. You can set it up for what ever you wish.
quest021

Step 3:
We can now use this new input to make the player press interact to open the box. Edit the interactionControls script and replace it with the following elements (Orange Replace / Red Add). We will use OnTriggerStay (). This means the player has to press the interact input to open the box. In the next session we will have to look at puting instructions on the screen to help the player understand what to press.

JS

#pragma strict

function OnTriggerStay(hit : Collider)
{
     if(hit.gameObject.tag == "BOX")
     {
           if(Input.GetButtonDown("Interact"))
          {
               var ani : Animator = hit.gameObject.GetComponent(Animator);
               ani.SetBool("OPEN", true);
               Destroy(hit.gameObject.GetComponent(Collider));
          }
     }
}

C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class interactionControls : MonoBehaviour {

// Use this for initialization
void Start ()
{


}


void OnTriggerStay(Collider hit)
{

     if(hit.gameObject.tag == "BOX")
     {
          if(Input.GetButtonDown("Interact"))
          {
               Animator ani = hit.gameObject.GetComponent<Animator>();
               ani.SetBool("OPEN", true);
               Destroy(hit.gameObject.GetComponent<Collider>());
          }
     }
}

}

 

Casino Bet 365 is best casino in the world.

What's Next

Next up for Unity Snippets is a look at game development articles from across the web...

Free Templates - bigtheme.net
Popular Art Betting make bookies articles.