Pick up a Key, Open a Door

From STEAMwiki
Jump to: navigation, search

A common game concept is to search for a key of some kind to open some kind of door. For the example below, the player cannot pass through the overgrown vines. Picking up the bowie knife allows them to cut through the vines and continue to the next area.

Set up your scene with 3 main objects:

  • The "ThirdPersonController" Ethan from the characters standard assets. Apply the tag "Player" to this GameObject.
  • A 3D object to use as the bowie knife. Name it in your scene "KeyQuest-1". Make sure to include a collider component on it.
  • Add an "Empty Child" to Ethan's hand, so we have a GameObject to attach the knife to. Name this "Key parent".
  • A 3D object of the vines blocking the path. Include a collider to block the character's path.


Player Key Obstacle setup.png

Add a script called "FirstQuest" to the KeyQuest-1 knife:

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

public class FirstQuest : MonoBehaviour {
	public Transform keyParent;

	// Use this for initialization
	void Start () {
		
	}

	// Update is called once per frame
	void Update () {

	}

	void OnCollisionEnter(Collision col)
	{
		if (col.gameObject.tag == "Player")
		{
			gameObject.transform.parent = keyParent.gameObject.transform;
			gameObject.transform.localPosition = new Vector3(-0.021f, 0.018f, -0.144f);
			gameObject.transform.localEulerAngles = new Vector3 (39.396f, 72.08601f, 137.002f);
		
			// Removes the rigidbody from the game object so that the player character isn't continuously colliding with it as they move around.
			Destroy(GetComponent<Collider>());
		}
	}
}

You will see that this script needs a reference to the "Key parent" GameObject in your scene, so drag that on to it.

This code will move the knife to the player's hand by assigning it's parent to be the empty GameObject "Key parent" that you added earlier. This code also changes the localPosition and localEulerAngles so that the knife is positioned properly in the hand. You may need to adjust the coordinates in those code lines to make your object line up properly.

Next, add a script called "FirstQuestPart2" to the blocking vines:

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

public class FirstQuestPart2 : MonoBehaviour {
	Transform keyObject;
	public Transform keyParent;

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}

	void OnCollisionEnter(Collision col)
	{
		if (col.gameObject.tag == "Player")
		{
			//Finds and assigns the child of the keyParent named "KeyQuest-1".
			keyObject = keyParent.Find("KeyQuest-1").gameObject.transform;

			//If the child was found.
			if (keyObject != null) {
				Destroy (gameObject, 0.1f);
				Destroy (keyObject.gameObject, 0.1f);
			}

		}
	}

}

This script also needs a reference to the "Key parent" GameObject in your scene, so drag that on to it.

This code checks to see if the knife, "KeyQuest-1" is attached as a child object to the "Key Parent" object. If it is, it destroys the vines and the knife since we don't need it any longer.

You could also modify this script to be generic for any quest objects by making public variables for them.

Credit for this code goes to Oliver from Unity camp July 2017. 3D objects sourced and set up by Tristan.