I have items around the map with the tag “Interactables” and I want to make a highlight around them when the raycast is hitting them. Is there anyway to do this? I’ve tried downloading an asset that gives a highlight but cant seem to figure out how to deactivate it when i’m not looking at it
When the scene starts, make a list of all the things you can highlight, such as by using FindObjectsOfType<>() or however you want to do it.
When you do the raycast, de-highlight all of them, then do the raycast to light up the one you might be touching.
I’ve tried that. i was able to get the raycast to start the highlight by simply calling the script and doing Enabled = true but I wasnt able to find a way to unhighlight it
I’m also trying to find a way to make it possible to have one script to use on different interactable objects instead of making a script for each one but i’m having trouble with that. i was following
https://www.youtube.com/watch?v=2LA3BLqOw9g
that video but I was having trouble with that as well
Here’s a script I’m using for my current project that does pretty much what you’re talking about. The gist is a I do a raycast every frame and keep track of what my raycast hit last frame. Then if the thing I’m hitting this frame is not the same as the thing I hit last frame, I know that I either just started or just stopped hovering over a particular object. I have a few extras in there like a maximum interaction distance and checking whether the Interactable is enabled.
I’m indeed using one script for all of my “interactable” objects. You don’t need to use interfaces for that at all. Just create a single “Interactable” component and put that on all of your interactable objects. Then use events to allow other scripts to listen for interaction events.
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.Rendering;
public class Interactor : MonoBehaviour {
public delegate void InteractableHoverHandler(Interactor source, Interactable hover, bool started);
public event InteractableHoverHandler OnInteractableHover;
public delegate void InteractionPerformedHandler(Interactor source, Interactable target);
public event InteractionPerformedHandler OnInteractionPerformed;
[SerializeField]
PlayerInput PlayerInput;
[SerializeField]
LayerMask RaycastMask;
[SerializeField]
Transform Cam;
InputAction interactAction;
RaycastHit lastHit;
Interactable lastHovered;
private void Start() {
interactAction = PlayerInput.actions.FindAction("Interact");
interactAction.performed += WhenInteractPerformed;
}
private void WhenInteractPerformed(InputAction.CallbackContext context) {
Ray r = default;
r.origin = Cam.transform.position;
r.direction = Cam.transform.forward;
bool wasHit = Physics.Raycast(r, out RaycastHit hit, 1000, RaycastMask.value, QueryTriggerInteraction.Collide);
Interactable interactable = wasHit ? hit.collider.GetComponentInParent<Interactable>() : null;
if (interactable && interactable.enabled && hit.distance <= interactable.MaxInteractionDistance) {
interactable.Interact(this);
OnInteractionPerformed?.Invoke(this, interactable);
}
else {
OnInteractionPerformed?.Invoke(this, null);
}
}
private void Update() {
Ray r = default;
r.origin = Cam.transform.position;
r.direction = Cam.transform.forward;
bool wasHit = Physics.Raycast(r, out RaycastHit hit, 1000, RaycastMask.value, QueryTriggerInteraction.Collide);
Interactable hoveredThisFrame = null;
if (wasHit) {
hoveredThisFrame = hit.collider.GetComponentInParent<Interactable>();
if (hoveredThisFrame && (!hoveredThisFrame.enabled || hit.distance > hoveredThisFrame.MaxInteractionDistance)) {
hoveredThisFrame = null;
}
}
if (lastHovered != hoveredThisFrame) {
if (lastHovered) {
lastHovered.EndHover(this);
OnInteractableHover?.Invoke(this, lastHovered, false);
}
if (hoveredThisFrame) {
hoveredThisFrame.StartHover(this);
OnInteractableHover?.Invoke(this, hoveredThisFrame, true);
}
}
lastHit = hit;
lastHovered = hoveredThisFrame;
}
}
Ok awesome thank you! I’ll definitely look into this! I’ll also learn more about events! So far I just made a separate script for one of the items I made and the other ones I’ll learn about events and work that into them and fix the one I already made!