I know this might need to go to the GUI as it does involve a lot of GUI aspects. However I would like to know if there is a way of, lets say talking to an NPC without making a on trigger box collider? Maybe click on the target perhaps ? If anyone would have any kind of insight to this matter then I would love to hear about it!!
Here is a small snippet of code to show you guys what I mean. Sorry for the lack of coding efficiency
using UnityEngine;
using System.Collections;
public class NPCInteraction : MonoBehaviour {
public string questTitle; //title
public string questInformation; //Quest information
public string[] playerAcceptAndDeclineButtons;
public string claimRewardInformation;
public string claimReward = "Claim your reward!";
bool displayDialog = false;
bool activateQuest = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnGUI()
{
GUILayout.BeginArea(new Rect(700, 600, 400, 400));
if(displayDialog && !activateQuest)//Display text if you havent spoken to the NPC before
{
GUILayout.Label(questTitle);
GUILayout.Label(questInformation);
if (GUILayout.Button(playerAcceptAndDeclineButtons[0]))//accept button
{
activateQuest = true;
displayDialog = false;
}
if (GUILayout.Button(playerAcceptAndDeclineButtons[1]))//decline button
{
displayDialog = false;
}
}
if(displayDialog && activateQuest)//Returning part of the quest
{
GUILayout.Label(claimRewardInformation);
if (GUILayout.Button(claimReward)) // claim reward button
{
displayDialog = false;
}
}
GUILayout.EndArea();
}
void OnTriggerEnter()
{
displayDialog = true;
}
void OnTriggerExit()
{
displayDialog = false;
}
}
You could use OnMouseUp() to activate on mouse clicks. Your NPC will still need a collider, but it probably has one anyway. You can also check the distance from the player to the NPC and only display the dialog if they’re close enough.
If you want to get fancier, use OnMouseEnter() and OnMouseExit() to change the NPC’s shader for a highlight-on-hover effect, or set/clear a UI Text that shows the name of the NPC you’re hovering the mouse over.
There are a million of ways to make you able to talk to an NPC, or other player or a stone without using a trigger. You could make a method that checks if you are facing the intended entity (raycast) and then listens for some form of input (which can be a button- or mouseclick, or a trigger, or any other event that could trigger the “TalkTo” event. This way you can also limit the distance in which the event can be activated.
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class NPCInteraction : MonoBehaviour, IPointerClickHandler {
//...
public void OnPointerClick(PointerEventData eventData)
{
displayDialog = true;
}
}
You might need the EventSystem component in your scene.
Is there a reason you are using OnGUI, not the new UI toolset? @Stardog is suggesting using EventSystems, which is closely related to the retained UI. I’d suggest getting your head around both Events and the UI system.