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