- Home /
press e to speek
I have this old NPC chat box system that was given to me. I do not know where to edit this to remove the chat box and just have a button floating above the NPC’s head. I would like the button to load a new level when clicked.
her the npc chat box system script
using UnityEngine;
using System.Collections;
//Put this script on your NPC
public class NPCTalkBox : MonoBehaviour
{
public string npcTextMessage = "Hello, I'm an NPC"; //Message to show on the screen
public Rect popupBox = new Rect(0.25f, 0.75f, 0.5f, 0.1f); //This is the size of your popup box in screen size
public Rect messageBox = new Rect(0.1f, 0.7f, 0.8f, 0.2f); //This is the size of your message box in screen size
public Transform player; //The player that we're checking the distance from
public float minDistance = 1f; //How far away we have to be in order to show the message
private bool inRange = false; //Controls showing the 'ready to talk' message
private bool showText = false; //Controls showing the NPC message
void Start()
{
//If you've forgotten to setup the player in the Inspector.. then I'm going to send you an angry message
if(player == null)
{
Debug.LogError("Variable 'player' not set up on "+gameObject.name+". Disabling the associated script.");
this.enabled = false;
}
}
void Update()
{
if(Vector3.Distance(transform.position, player.position) <= minDistance) //Are we close enough?
{
//Toggle showing the "press X to talk" message
inRange = true;
//Toggle showing the text every time we press Fire1
if(Input.GetButtonDown("Fire1"))
{
showText = !showText;
}
}
else
{
//Hide the text if we walk out of range
inRange = false;
showText = false;
}
}
void OnGUI()
{
if(inRange && !showText)
{
GUI.Box(new Rect(Screen.width*popupBox.x, Screen.height*popupBox.y, Screen.width*popupBox.width, Screen.height*popupBox.height), "Press to talk to "+gameObject.name+".");
}
if(showText)
{
GUI.Box(new Rect(Screen.width*messageBox.x, Screen.height*messageBox.y, Screen.width*messageBox.width, Screen.height*messageBox.height), npcTextMessage);
}
}
}
ples help
(I've published to the site - I suspect the OP will reword the question)
maybe, ins$$anonymous$$d of checking if your in range, then checking if you pressed E, it should be check to see if in a collider(set as trigger) (this will be your range checker) AND (so its a "&&" )if youve pressed E, then do it.
Also im not sure if GUI can go above peoples heads as its restricted to a 2D space and only on your view port. Perhaps use 3D Text and edit the mesh in script?
just a thought
Could you tell me what to edit and where it should go because I have some questions? If we just had to press the ‘E’ key then I would have to rewrite the script to say “press e to interact”. Of course, I can definitely do that, but my biggest problem is that I am scared about the whole range thing. I don’t want people to be able to keep pressing the button to go to the next level.
i am im going be editing some of my post tonite wan i get home and have my dragon softwer on hand but why did you get rid of my dislexsya point i sead that so ppl dont think im perpisly doing this
“i have this old npc chat box system that wus given by my frend who is no longer my frnd eney wahy i dont to edit this wer i get rid of the chat box in genral and gust have the floting buten on top of ther head and wan you click it it will tp you no a new level.”
I have this old NPC chat box system that was given to me. I do not know where to edit this to remove the chat box and just have a button floating above the NPC’s head. I would like the button to load a new level when clicked.
“wall cold you tall me wer and wut i wold put in to do that? but this duse bring up some qrechon if we wold have to gust do the e key then i wold have to rewrite the script to say pret e to interact of cors i can defintly do but my bigis problum im scerd about is the hole range thing in wich i dont wont ppl to gust keep presing e to tp to the next level”
Could you tell me what to edit and where it should go because I have some questions? If we just had to press the ‘E’ key then I would have to rewrite the script to say “press e to interact”. Of course, I can definitely do that, but my biggest problem is that I am scared about the whole range thing. I don’t want people to be able to keep pressing the button to go to the next level.