How do I make an object disappear when the player can't see it.

I want to make an object disappear when the object is not visible to the player.I don’t want it to turn its visibility back again.I just want it to disappear ones and not come back.

Unfortunately there is no method like ‘canBeSeenByPlayer’ (which would really be useful).

Maybe you could use findLookAtRotation(other actor) and then check if the absolute of the yaw difference is greater than a specific threshold.

Just looking at the rotation is not enough.
Maybe the object iswell within the FOV, but occluded by another object in the foreground.
You would need to do a trace/sweep as well to check if the object is indeed visible, or not…

I Thought about something like this too.But I simply couldn’t check if the object hit was the one I wanted.I tried physical material and all.But It didn’t work.Made me feel really bad.Thanks for the help.

KVolgler is completely right, forgot to add this.
So you could just do a line trace from the camera like this

https://docs.unrealengine.com/latest/images/Gameplay/HowTo/UseRaycasts/Blueprints/GHT2B_15.png

… and then check if the first blocking hit is ‘your’ actor.

It’s recommended to not do this every frame by the tick event but to use a timer instead (and trigger the code 5-10 times/sec. or so).

but wouldn’t this just make the object disappear as soon as it is I look at something else?I want it to disappear when It is not inside my vision.

This code alone doesn’t make your actor disappear. But you could modify it so that the actor gets hidden if

  1. the hit actor equals ‘your’ actor
  2. the absolute of the yaw rotation delta > your treshold

After the object was hidden/destroyed you could just could stop the timer.

Thank you very very much.If you could help me with those 3.I would really appreciated.I have already gotten to this far before but simply couldn’t do those 3.

Hi SaitYavuz,

ok, I attached a test project. Note that the treshold for the ‘outside of viewing angle’ function should be tweaked manually to fit your need. It’s not a perfect elegant solution for all kind of objects, because it only tests again the target actors pivot point and not it’s outer bounds.

link text

I dont know how efficient or practical it would be but you can also use a HUD BP to find out if an actor is on the screen or not. Event Receive Draw HUD already gives you the screen size(Size X and Y) so what you do is:

  • Get a reference to the actor you want disappear > Get its location.
  • Use Project World to Screen node to turn it into screen space coordinates.
  • Compare those values with screen size values to see if the object is on the screen.
  • If not, destroy the actor.

You can offset those values by distance to make sure destroy function executes only when the whole character is out of the screen instead of just it’s pivot point.

Thanks a lot man.Couldn’t have done this without you!