Godot Version
4.3
I’m building a top down game with an orthogonal camera. I am trying to detect a tile when the mouse hovers over it and having issues with the detection.
The tile is an Area3D node with a 3DCollisionShape:
The tiles display their position (rounded).
The camera scene is a CharacterBody3D with a Camera3D and RayCast3D nodes (the collision shape is disabled):
All nodes are at default position and rotation other than RayCastingCamera where rotation.X = -90 so it points downward.
GUI scene (main) is a Node3D with the overhead camera positioned at (0,8,0). The tiles are added via code at positions (x,0,z):
Camera code:
public partial class OverheadCameraBody : CharacterBody3D
{
private RayCast3D downwardRay;
private Camera3D rayCastingCamera;
private GodotObject detectedCollider = null;
public override void _Ready()
{
rayCastingCamera = GetNode<Camera3D>("RayCastingCamera");
downwardRay = rayCastingCamera.GetNode<RayCast3D>("DownwardRay");
}
public override void _Process(double delta)
{
detectedCollider = CastMouseRay();
}
public override void _Input(InputEvent @event)
{
if (Input.IsActionPressed("selection"))
{
if (detectedCollider != null)
{
GD.Print("Selected object is: " + detectedCollider.ToString());
} else {
GD.Print("No collider detected");
}
}
private GodotObject CastMouseRay() {
// Getting the mouse position on the viewport
Vector2 mousePosition = GetViewport().GetMousePosition();
// Transalting the mouse position to world position and assigning to camera target
downwardRay.TargetPosition = rayCastingCamera.ProjectPosition(mousePosition, Position.Y + 20.0f);
downwardRay.ForceRaycastUpdate();
return downwardRay.GetCollider();
}
}
The ray’s position and target are where I expect them, and align with the position of the tiles, but the tile is not detected.