When Near a door, Press a button to go to the other side.
I'm still moderate on the coding side of unity. I was wondering if its possible to go near a door, press a button and appear on the other side. Just like in Silent hill and the resident evil games.
Answer by ben-rasooli · Apr 25, 2016 at 12:32 PM
One way to do this is to make a trigger zone in front of your door and wait for the player to enter it. To make a trigger zone, use a cube as shown in the picture, untick its Mesh Renderer and tick Is Trigger in the Box Collider of it.
You need to make scenes for each room and load each one when player presses the button. Give a tag name to your player's GameObject (in this case I used "Player") so that the trigger zone can recognise the player. Then attach the following script to your trigger area:
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class SceneTransitionArea : MonoBehaviour {
bool _isPlayerWithinZone = false;
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
_isPlayerWithinZone = true;
}
void OnTriggerExit(Collider other)
{
if (other.tag == "Player")
_isPlayerWithinZone = true;
}
IEnumerator watchForKeyPress()
{
while(_isPlayerWithinZone){
if (Input.GetKey(KeyCode.Return)) {
SceneManager.LoadScene("nextDoorSceneName");
}
yield return null;
}
}
}
In this case, when the player is in the trigger zone and presses Return/Enter on the keyboard he will go to the next door. All you need to change in this code is the nextDoorSceneName and set it to whatever scene you want to load next.