- Home /
Is there a way to make this a button press?
I have found a scrip that changes levels when the player gets near a door. However I would like it to be a Key press. Is there a way for me to change this script so that I can press E when i get close to change levels?
Thanks.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;
public class LevelControl : MonoBehaviour { public string levelName;
void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
SceneManager.LoadScene(levelName);
}
}
}
Answer by Atiyeh123 · Oct 28, 2018 at 03:38 PM
Hi,
you can do this with a global variable
bool triggerPlayer = false;
void Update()
{
if(Input.GetKeyDown(KeyCode.E))
{
if(triggerPlayer)
SceneManager.LoadScene(levelName);
}
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
triggerPlayer = true;
}
}
void OnTriggerExit2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
triggerPlayer = false;
}
}
Thanks for that mate. Also I just wanted to know something else. On my hub which is where i go through the doors I have a game object that hides the mouse but in the other levels I dont. When i go to the other levels using the door i still have no mouse but changing scenes to work on them is fine and shows the mouse like i want it to. Should i send the script for the mouse cursor or is there a simple way around it?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
how to add x value in y seconds 1 Answer
Changing a text equal to a string variable 1 Answer
How can i find a thirdpersoncontroller hand and attach object to the hand ? 1 Answer