Getting world direct space state in C#

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By profjle

The docs claim that var spaceState = GetWorld().DirectSpaceState; works for this but it seems out dated. I have tried World.DirectWorldSpace and it doesn’t work either. Not sure what else to do in this case as I’ve gone through the World class and just makes sense that it should work.

:bust_in_silhouette: Reply From: crogaro

I also had problems with this, this is the code I used. It worked with GetWorld().DirectSpaceState. The class might need to a child of grand child of the Spatial class.

float timer = 0f;
    public override void _PhysicsProcess(float delta)
        var direct_state = GetWorld().DirectSpaceState;
        var collision = direct_state.IntersectRay(Transform.origin, new Godot.Vector3(0,0,-2));      
        if(collision != null)
            if(timer >1f)
                timer = 0;
            timer = timer + delta;

I believe GetWorld().DirectSpaceState can only be called inside of the _PhysicsProces() function, although I’m not too sure about that.

Also, I’ve had issues with different environments acting strangely, for example, collision.Position doesn’t work on one machine but does on the other, maybe mono install issue or dotnet core? I’m not sure about that ether.

Yes, it does have to go under _PhysicsProcess(). Def made that mistake in my reply to myself. I got it working though.

For collision.Position try collision[“position”]. Intersect array will return a dictionary and this should return that property. That goes for all of the properties inside the dictionary mind you. You can see all of the properties you can access here if you wanna take a peak. Scroll down to the “The result dictionary…” line.

profjle | 2021-06-23 22:50

Ahh thank you!

Just needed to change the object type from Node → Spatial

dezboyle | 2022-04-09 15:38