- Home /
I want to setup a raycast that is going forward from my character that allows me to left click on a cube with the tag "npc" and open up a gui window (in C#)
This is for a third person camera view from behind the character/over the shoulder. My raycast is supposed to be aimed forward from my character at about mid height so that if I come across a merchant I can left click on him and it will ignore my character and check whether the npc is a certain distance from me and whether it possesses the tag "npc" which will open up the gui window with all the goods to sell.
there are still a few variables floating around in this script because i'm still editing :P.
any help would be appreciated.
using UnityEngine;
using System.Collections;
public class ClickHandler : MonoBehaviour {
public Transform playerTarget;
public Transform activateRange;
public float chatDist;
public string tagCheck = "Npc";
bool checkAllTags = false;
public Texture2D chatBG;
public Texture2D shopBG;
public bool foundHit;
public bool shop = false;
public bool endShop = false;
public bool talking = false;
public TextAsset dialogueText;
string displayText;
public GUIStyle textStyle = new GUIStyle();
// Use this for initialization
void Start () {
foundHit = false;
checkAllTags = false;
talking = false;
displayText = dialogueText.text;
textStyle.fontSize = Screen.height / 60;
textStyle.normal.textColor = Color.white;
}
// Update is called once per frame
void Update () {
RaycastHit hit;
if (Input.GetMouseButtonDown(0)) {
foundHit = Physics.Raycast (playerTarget.transform.position, activateRange.transform.position, out hit);
if (foundHit && hit.transform.tag != "npc") {
foundHit = false;
}
if (foundHit && hit.transform.tag == "npc") {
talking = !talking;
}
}
}
void OnGUI(){
if (talking == true) {
GUI.DrawTexture (new Rect(Screen.width/2 - Screen.width / 4, Screen.height / 11, Screen.width / 5, Screen.height / 3), chatBG);
GUI.Box (new Rect(Screen.width / 4 + Screen.width / 60, Screen.height / 8, Screen.width / 5, Screen.height / 4), displayText, textStyle);
GUI.Button (new Rect(Screen.width / 2 - Screen.width / 5 - Screen.width / 30, Screen.height / 2 - Screen.height / 7, Screen.width / 14, Screen.height / 32), "Thankyou!");
if (GUI.Button (new Rect(Screen.width / 2 - Screen.width / 7 + Screen.width / 150, Screen.height / 2 - Screen.height / 7, Screen.width / 14, Screen.height / 32), "Go to hell!")) {
talking = false;
}
}
}
}
what I have found is that i go into game and i move back a rather far distance and it finally checks the foundHit but wont activate foundHit if i'm any closer or further. another thing I've noticed is that the hit.transform.tag == "npc" isn't picking up the object tag.
Answer by killerstreak · Aug 27, 2014 at 04:22 PM
not even gonna read the code here because I am novice (or lazy). but wouldnt a trigger sphere around the merchant be a better idea? like when you walk inside of it.. you can click it..
like this script could be attached to the merchant
var clickable : boolean;
function OnTriggerStay3D(other : Collider3D)
{
if(other.gameObject.tag=="Player")
{
clickable == "true";
}
else
{
clickable == "false";
}
}
function OnMouseDown()
{
if(clickable == "true")
{
//open gui window
}
}
function Update()
{
if(clickable == "false")
{
//close gui window
}
}
alot less code ;)
sry for any syntax mistakes I might have made
Answer by Cherno · Aug 28, 2014 at 09:27 AM
This should do the trick: It offsets both origin and target vector of the ray by 0.5 upwards.
foundHit = Physics.Raycast (playerTarget.transform.position + Vector3.up * 0.5f, playerTarget.transform.forward + playerTarget.transform.position.up * 0.5f, out hit);