Search Unity

Looking for improvement on my solution to show/hide labels on player proximity to objects

Discussion in 'Scripting' started by Sendatsu_Yoshimitsu, Sep 20, 2014.

  1. Sendatsu_Yoshimitsu

    Sendatsu_Yoshimitsu

    Joined:
    May 19, 2014
    Posts:
    691
    I'm building the looting system for my (third-person) game, right now it mechanically involves placing the aiming reticule over the object the player wishes to interact with and hitting the appropriate key. If the object can be interacted with I want to indicate this visually, currently I have a glow-y shader and an item name label which both pop up if the object can be taken/used.

    Now, making the visuals appear is trivial, I just cast a line through the reticule and make a check against whatever it collides with to see if that object is interactable, and if it is I activate the shader and label. Where I'm stuck is making both disappear when the reticule leaves the object. Right now I'm doing it with caching: each time the linecast gets a hit it caches whatever it's looking at, and every frame it checks to see if it's either not getting a hit, or getting one from an object other than what's in the cache, and if either one is true it disables the labels on the cached object.

    This works, but it feels extremely clunky, and I don't like checking against my cache every frame. Is there either a method I'm unaware of to detect a linecast no longer touching an object, or a much simpler implementation that I skipped over entirely?
     
  2. BmxGrilled

    BmxGrilled

    Joined:
    Jan 27, 2014
    Posts:
    239
    if the reticle is always in the middle of the screen you can use:

    OnMouseEnter() {
    }
    and
    OnMouseExit() {
    }

    inside of a script on the interactive object

    note: the object has to have a collider on it, my assumption is that because you're using raycasts they already do

    I hope this helps! :)
     
  3. Sendatsu_Yoshimitsu

    Sendatsu_Yoshimitsu

    Joined:
    May 19, 2014
    Posts:
    691
    Oh hey, that works! Thank you so much, this's been bugging me all week! :)
     
    Last edited: Sep 22, 2014