Problem getting wanted result from raycast

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

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;
    }
}

:bust_in_silhouette: Reply From: Andrea

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

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

:bust_in_silhouette: Reply From: miivers

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);
    }
}