0 votes

Hello

I am still ferly new to GoDot and I am having some issues with raycast. I basically want to get the mouse position when hovering over a 3d plane. My issue is that I am getting the raycast hit result on strange positions. I have drawn an orange rectangle in the screenshot below to illustrate where I am getting my hit results. I am drawing a sphere 10 units along my raycast direction to verify that my "to" position is correct which it is.
The debug sphere is behind the red plane in this screenshot. I am also printing the mouse x and y coordinates which looks correct. What am I missing here?

My code looks like this:

using Godot;
using System;

public class GameCamera : Camera
{
    const float rayLength = 10;
    RayCast rayCast = null;
    Spatial debug = null;

    public override void _Ready()
    {
        rayCast = new RayCast();
        rayCast.Enabled = true;
        AddChild(rayCast);

        rayCast.CollideWithBodies = true;
        rayCast.CollideWithAreas = true;

        debug = GetNode("debugSphere") as Spatial;

        UpdateCastToPosition();
    }


    public override void _PhysicsProcess(float delta)
    {
        UpdateCastToPosition();
        Godot.Object result = rayCast.GetCollider();

        if (result != null)
        {
            GD.Print("Hit!");
        }
    }


    void UpdateCastToPosition()
    {
        var mousePosition = GetViewport().GetMousePosition();
        GD.Print($"{mousePosition.x}, {mousePosition.y}");

        var from = ProjectRayOrigin(mousePosition);
        var to = from + ProjectRayNormal(mousePosition) * rayLength;

        //rayCast.Translation = from;
        rayCast.CastTo = to;

        var foo = debug.GlobalTransform;
        foo.origin = to;
        debug.GlobalTransform = foo;
    }
}

enter image description here

Godot version Godot Engine v3.2.3.stable.mono.official
in Engine by (34 points)

2 Answers

0 votes

i also had some issues using the ProjectRay functions, it turned out a better function is projectposition (and the inverse, unprojectposition)

try withfrom=camera.translation and to=project_position(mouse_position, raylenght)

by (1,514 points)
0 votes

So I finally figured out my problem. In short I was mixing coordinates between local and global coordinate system.

Turning on "Debug -> Visible collision shapes" helps a lot!

This is my final code:

using Godot;
using System;

public class GameCamera : Camera
{
    const float rayLength = 10;
    RayCast rayCast = null;

    public override void _Ready()
    {
        rayCast = new RayCast();
        rayCast.Enabled = true; //Why is this not default true?
        AddChild(rayCast);

        rayCast.CollideWithBodies = true;
        rayCast.CollideWithAreas = true;

        UpdateCastToPosition();
    }


    public override void _PhysicsProcess(float delta)
    {
        UpdateCastToPosition();
        Godot.Object result = rayCast.GetCollider();

        if (result != null)
        {
            GD.Print($"Hit at position {rayCast.GetCollisionPoint().ToString()}");
        }
    }


    void UpdateCastToPosition()
    {
        var mousePosition = GetViewport().GetMousePosition();

        var from = ProjectRayOrigin(mousePosition);
        var to = from + ProjectRayNormal(mousePosition) * rayLength;

        rayCast.Translation = rayCast.ToLocal(from);
        rayCast.CastTo = rayCast.ToLocal(to);
    }
}
by (34 points)
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 Frequently asked questions and 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 [email protected] with your username.