How to hide objects between player and camera?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Nuno Donato
:warning: Old Version Published before Godot 3 was released.

My camera follows my player (3rd person view) at a distance, so when objects are in between both, I’d like to hide them (or reduce transparency).

My first attempt was to use a raycast that originates in the camera and targets the player, and get its collisions.

The problem with this is that it only returns one collision at a time. I then add that object to the raycast exception list so that he gets the next one. So far so good.

The bigger problem is that with this method I have no way to know if the objects in the exception list are still colliding with the raycast or not, so that I could show them again when the player moves out of that place.

I tried setting up a timer to remove all exceptions and get all collisions, but then I get flickers :frowning:

Any ideas on how to go about doing this?

thanks in advance!

Are you using the RayCast node, or are you raycasting directly?

Bojidar Marinov | 2016-03-09 11:12

I used a raycast node

Nuno Donato | 2016-03-09 11:17

:bust_in_silhouette: Reply From: Bojidar Marinov

The best way to do this would be using direct space raycasts (in _fixed_process), as explained in the raycast tutorial. Then you might go like this:

var last_obstructing_objects = []
func _fixed_process():
    var obstructing_objects = []
    var space_state = get_world().get_direct_space_state()
    while true:
        var collision_result = space_state.intersect_ray(camera_pos, player_pos, obstructing_objects)
        if collision_result.has("collider") and !collsion_result.collider extends preload("player.gd"):
            obstructing_objects.push_back(collision_result.collider)
        else:
            break # No more collisions/collided with player
    last_obstructing_objects.show() # Would error, implement custom logic here
    obstructing_objects.hide() # Same here
    last_obstructing_objects = obstructing_objects

awesome, thanks :slight_smile:
extra question, does it make much difference to place this is fixed_process instead of process? Maybe it doesnt matter, but wont it help performance by putting it in process (by casting less rays?)

Nuno Donato | 2016-03-09 12:07

I matters ALOT, since outside of _fixed_process, you can’t get the direct space state.

Bojidar Marinov | 2016-03-09 13:31