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.