ShapeCast3D not detecting CharacterBody3D

Godot Version

v4.3.rc1.mono.official [e343dbbcc]

Question

Hello everybody, I have an issue with a ShapeCast3D not detecting my player character (it is controlled by a script that inherits from CharacterBody3D).

I have a “sight” function that checks the collision result of the ShapeCast3D to update a target variable and it is not detecting my player. Curiously, I was calling that sight function from _Process and changed it to _PhysicsProcess and it worked!.. but only for a while. Now, is broken again (still called by _PhysicsProcess).

I guess it is somehow related to that my player is controlled, but I can’t figure out what to do. Have you ever had this issue?

Thank you for your help.

PD: Just in case it is useful, I use C#.

can you show the code of your “sight”-method?

Sure:

    public void UpdateSight()
    {
        sightShapeCast3D.ForceShapecastUpdate();
        Debug.Print(""+sightShapeCast3D.CollisionResult);

        if (sightShapeCast3D.CollisionResult.Count > 0)
		{
            foreach (Dictionary result in sightShapeCast3D.CollisionResult)
            {
                itarget = (ITarget)(Node)result["collider"];
                Debug.Print("--------------------Target found");
                newTarget.Invoke();
            }
        }
    }

I tried using ForceShapecastUpdate and without it. When it worked, it worked with and without it.

and did you change anything about the player or the shapecast before it stopped working?

I don’t think so… It didn’t work at the beginning and then I moved the call to that function from _Process to _PhysicsProcess in the class that calls it, and it worked. I don’t understand why. But after keep working on several code parts it stopped working… Then I moved back this function call to _Process just in case, and nothing again.

My code structure is simple:

Enemy class has a AIPerception component, which contains a reference to the ShapeCast3D and has the function I shared.

It’s worth to mention that this code works fine detecting a dummy CharacterBody3D I created to test:
image
The script DummyTarget contains is very simple, it is basically a script that inherits from ITarget because that is the class my Sight function looks for.

Dummy and real player are both in the same Collision layer.

Well physics-calculation should always be on _physicsProcess. ForceShapeCastUpdate should also not be necessary.
And you are sure it is in range of the shapecast?

I found what happened! Human error as expected!

A bitwise operation mistake :face_exhaling:.
The commented line is the one I had first, and the second line what it had to be.

I was setting the CollisionLayer as declaring enum flags…

// CollisionLayer = 1 << Globals.playerLayer;
CollisionLayer |= Globals.playerLayer;

Thank you for your help, and I am so glad it works now!

Also thank you for your advice with _physicsProcess.

PD: If anyone reads this in the future, when casting, be sure about your collision layers and masks!

1 Like

Oh yeah bitwise operations also give me a lot of troubles :sweat_smile:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.