Player: UI Info Overlay
Solution to displaying interactable object information on `UI based on https://forum.unity.com/threads/get-ui-placed-right-over-gameobjects-head.489464/

//send the ray from the **CAMERA** (not the player) rayDirection = camera.transform.TransformDirection(Vector3.forward); //point of origin is LOOK - from the camera ray_pointOfOrigin = camera.transform.position; if (Physics.Raycast(ray_pointOfOrigin, rayDirection, out interactableObjectHit, scanRange)) { //Change the color based on object in range //Check if the hit obj has Interactable Tag if (interactableObjectHit.transform.CompareTag("Interactable")) { crosshair.color = Color.red; //Get the transfrom of the hit object rayCastHitObject_transform = interactableObjectHit.transform; //Get the name of the hit object rayCastHitObject_name = interactableObjectHit.transform.name; if (interactableObjectHit.transform == null) { rayCastHitObject_name = null; } } } else { crosshair.color = Color.white; //change the name to null so we can differentiate a //no hit event in the UI controller rayCastHitObject_name = null; }
/* RAYCAST INFO Running in update to ensure we are updating what we're looking at. We are receiveing raycast info from playerinteraction script that gives us the name of the object we've hit (we are looking at) using: rayCastHitInfo = hit.transform.name; We can use this info in the UI to display the name and info about the object we're currently looking at. *********************************************************************/ if (playerInteraction.rayCastHitObject_name != null) { //Turn on the components we need in UI canvasGroup_interactableObjUI.enabled = true; panel_interactableObjUI.enabled = true; text_interactableObjUI.enabled = true; lineRenderer_hitObjectToUI.enabled = true; //add the line lineRenderer_hitObjectToUI.startWidth = 0.001f; lineRenderer_hitObjectToUI.endWidth = 0.01f; //Set up a new V3 to hold the position of the UI element: Vector3 UI_element_transform = new Vector3( //x and y will attach to pivot points of the UI object set in the Inspector text_interactableObjUI.rectTransform.position.x, text_interactableObjUI.rectTransform.position.y, //Add 1.0f on z else we won't see the line 1f ); //No. of points on the line (start/end = 2) lineRenderer_hitObjectToUI.positionCount = 2; //start at position of the UI obj (rayInfo) and change it to World position lineRenderer_hitObjectToUI.SetPosition(0, cam.ScreenToWorldPoint(UI_element_transform)); //second point is position of the object hit by the raycast in playerInteraction lineRenderer_hitObjectToUI.SetPosition(1, playerInteraction.rayCastHitObject_transform.position); /********************************************************************* Add the text -this needs to pull in pre stored text like a databasethat depends on the name of the hit object - playerInteraction.rayCastHitObject_name *********************************************************************/