im trying to show ui when looking at object from 2 units away
So i have these scripts
//---- moved to player using UnityEngine;
public class PlayerCasting : MonoBehaviour {
public static float DistanceFromTarget; // contains how far we are from target (door)
public float ToTarget;
void Update()
{
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit))
{
ToTarget = hit.distance; // target distance = variable to target (float)
DistanceFromTarget = ToTarget; // static variable = target
}
}
}
//----moved to switch 001, moved text to here too using UnityEngine; using UnityEngine.UI;
public class OpenDoor001 : MonoBehaviour { public GameObject TextDisplay; public float TheDistance = PlayerCasting.DistanceFromTarget; // brings in value from PlayerCasting script
void Update()
{
TheDistance = PlayerCasting.DistanceFromTarget;
}
void OnMouseOver() // when mouse hovers over switch
{
if (TheDistance <= 2) // if player is less than 2 distance away from switch
{
TextDisplay.GetComponent<Text>().text = "Press Button"; // displays text
Debug.Log("youre looking at the switch");
}
}
void OnMouseExit() // if mouse isnt hovered over switch
{
TextDisplay.GetComponent<Text>().text = ""; // wont show any text
}
}
i want the player to see a text pop up when looking at the object to say ''press button'' but for some reason my distance from object when selecting the object and looking at the distance box in inspector, the distance is always stuck at 216.1 no matter where the player is in the map
i thinkj the reason is that the script cant detect my position in the world and is locked to something else but i cant figure out what im doing wrong
Good day.
Can you post images of inspector of camera and canvas?
Bye!