+5 votes

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 :(

Any ideas on how to go about doing this?

thanks in advance!

in Engine by (289 points)

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

I used a raycast node

1 Answer

+5 votes
Best answer

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
by (1,586 points)
selected by

awesome, thanks :)
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?)

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.