In Unity 5.4.1 I'm trying to add a tag that says 'press E to open' to a door as you approach it. I've got a RayCast pointing forward from the player, detecting the door and , connected to my PlayerController, I've got a copy of a prefab GameObject which itself contains a Text UI element. The GameObject _interactionTag is instantiated into the scene when the player is close enough.
But I'm having some trouble getting it to display correctly in the scene and I'm not sure why.
Here are the relevant parts of my PlayerController:
public class PlayerController : MonoBehaviour {
public Canvas _canvas;
private Vector3 _fwd;
private RaycastHit _hit;
public GameObject interactiveTag;
private GameObject _interactiveGameObject = null;
private GameObject _interactionTag;
void Update () {
_fwd = transform.TransformDirection(Vector3.forward);
if (Physics.Raycast(transform.position, _fwd, out _hit, 3)) {
if (_hit.distance<1) {
_interactiveGameObject = _hit.collider.gameObject;
GameObject _interactionTag = (GameObject) Instantiate(interactiveTag, _interactiveGameObject.transform.position, _interactiveGameObject.transform.rotation);
_interactionTag.GetComponentInChildren<Text>().text = _interactiveGameObject.GetComponent<IsInteractive>().interactionText; // this gets a text string from the door 'press E to open'
_interactionTag.transform.SetParent(_canvas.transform, false);
}
}
}
In game, the _interactionTag is appearing - though it's backwards and does not appear attached to the door. Also is any of this the best way of getting the 'press E to open' tag to appear attached to the center of the door? I'm beginning to wonder if what I'm doing is best practise. Thanks for any help.