0 votes

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.

Godot version Godot 3.2.3 Stable.Mono
in Engine by (26 points)
retagged by

1 Answer

0 votes

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;
                GD.Print(direct_state);
                GD.Print(collision);
            }
            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.

by (14 points)

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.

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.