void Update() { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit, 100f, LayerMask.value)) { var gameObject = hit.collider.gameObject; // NB: only for blocking interactions. if (gameObject.tag == "InteractionCollider") { return; } if (gameObject != currentHover) { currentHover = gameObject; OnHoverUpdate.Invoke(currentHover); } } else { if (currentHover != null) { currentHover = null; OnHoverUpdate.Invoke(null); } } }
private void UpdateItemInView() { if (enableDebug) { Debug.DrawRay(camComp.transform.position, camComp.transform.forward, Color.green); } //Perform ray cast to see if there are any interactables in front of the player RaycastHit RayHit; bool rayHit = Physics.Raycast(camComp.transform.position, camComp.transform.forward, out RayHit, interactRange, layerMask); if (rayHit) { GameObject HitObject = RayHit.collider.gameObject; if (HitObject != itemInView) { //Update interactable in view if valid and return Interactable interactable = HitObject.GetComponent <Interactable>(); if (interactable) { itemInView = interactable; } else if (itemInView != null) { itemInView = null; } OnHoverUpdate?.Invoke(itemInView); } } else if (itemInView) { //Clear the last item in view itemInView = null; OnHoverUpdate?.Invoke(itemInView); } }