5: Scripting the Box to open.

 Step 1:
We need to add a trigger to the game object so that we know when the player is in fornt of the box.
quest017

We do this by adding a box collider component in the inspector window and checking the Trigger box.
quest018

Finally we need to tag this object as BOX. You will need to create a new tag by adding a tag to the tag list.
quest019

Step 2:
Create a new script called interactionControls and add the script below. We will add to this script later in order to control UI elements and making the player interact with the object by pressing a button to open.

JS

#pragma strict

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

C#

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

public class interactionControls : MonoBehaviour {

// Use this for initialization
void Start ()
{


}

void OnTriggerEnter (Collider hit)
{

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

}

 

Now save and test the script by walking into the box, the lid should open.

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.