Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How To Make it so that whenever a person hovers over a certain object, it displays its name and when

SoulBlazeSoulBlaze Member
edited June 2021 in Programming

Guys I am trying to make a game like __Granny__for pc and want that whenever a person hovers over a certain object , it displays its name and when he exits hovering that object the object name goes away....Please guys help me... i need that when hovering over object ...it shows its name (i have done this by making a text object enabled or disabled) And when i hover over a object, it indeed displays its name but the text starts twinkling really fast....btw

i used raycasts to the logic and even tried using OnMouseOver() and OnMouseExit() but still the same problem....if you have any solution to this then please help me...

Here is The Script I am using---------------------------------------------------------------------------------------

  1. ~using UnityEngine;
  2. using UnityEngine.UI;
  3. public class LookForAObject : MonoBehaviour
  4. {
  5. [SerializeField] private bool IsDisplayTextActive;
  6. RaycastHit hit;
  7. public Text ObjectInfo;
  8. [SerializeField] private Camera raycastingcam;
  9. [SerializeField] [Range(0 , 300)] private float cameraCastRange = 50f;
  10. void Start()
  11. {
  12. ObjectInfo.text = "This is a " + hit.transform.name;
  13. ObjectInfo.gameObject.SetActive(false);
  14. Debug.Log(ObjectInfo.text);
  15. }
  16. void Update()
  17. {
  18. LookForObjects();
  19. }
  20. void LookForObjects()
  21. {
  22. if(Physics.Raycast(raycastingcam.transform.position, raycastingcam.transform.forward, out hit , cameraCastRange ))
  23. {
  24. if(hit.transform.CompareTag("Veiwable"))
  25. {
  26. ObjectInfo.gameObject.SetActive(true);
  27. ObjectInfo.text = hit.transform.name;
  28. IsDisplayTextActive = true;
  29. }
  30. else
  31. {
  32. ObjectInfo.gameObject.SetActive(false);
  33. ObjectInfo.text = "";
  34. IsDisplayTextActive = false;
  35. }
  36. }
  37. }
  38. }

~

Sign In or Register to comment.