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)
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.