Hello friends,
I am trying to create pop up image with GUI TEXTURE when player goes near to the door or any button using raycast, but I want to give the (x,y) coordinate of that texture such that the gui texture appears near to that door or button but I am unable to do this, I am using Input.mouseposition function to give (x,y) coordinate but its not proper.
What should I do for this, WorldToScreenPoint and ScreenToWorldPoint are these function useful? I am not getting how to use these functions effectively. Please help me to achieve this.
var PopUpTexture : Texture2D;
var MaxInteractDistance : float = 5.0;//how far the player may be when looking at a button or door for the popup to appear
var ShowPopUp : boolean;
var ObjectUnderRay : GameObject;
function Update()
{
var ray : Ray = Camera.main.ViewportPointToRay (Vector3(0.5,0.5,0));
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, MaxInteractDistance))
{
if(hit.collider.gameObject.CompareTag("Door") || hit.collider.gameObject.CompareTag("Button"))
{
ObjectUnderRay = hit.collider.gameObject;
ShowPopUp = true;
}
else
{
ShowPopUp = false;
ObjectUnderRay = null;
}
}
}
function OnGUI()
{
if(ShowPopUp == true)
{
var WorldPos : Vector3 = Camera.main.camera.WorldToScreenPoint(Vector3(ObjectUnderRay.transform.position.x, ObjectUnderRay.transform.position.y, ObjectUnderRay.transform.position.z));
GUI.DrawTexture (Rect (WorldPos.x - PopUpTexture.width / 2, Screen.height - WorldPos.y - PopUpTexture.height / 2, PopUpTexture.width, PopUpTexture.height), PopUpTexture);
}
}