Godot 4.3
Using Better Terrain Addon
I’m trying to draw a raycast between the mouse’s current position and it’s last position so I can draw in any missing tiles in case you draw too fast (QoL). I know early optimization and stuff can be harmful but this is pretty important ngl.
Here’s my code, and context…
A simplelon (or whatever they’re called) that defines mouse position stuff, called global.gd.
extends Node
var global_mouse_pos = Vector2(0, 0)
var last_global_mouse_pos = Vector2(0, 0)
func _process(_delta: float) -> void:
last_global_mouse_pos = global_mouse_pos
global_mouse_pos = get_viewport().get_mouse_position()
…now that I look at it, why did I ever define global_mouse_pos? Whatever.
Here’s my raycasting script…
extends Node2D
var last_cam_mouse_pos = Vector2(0, 0)
var cam_mouse_pos = Vector2(0, 0)
func _physics_process(delta):
var space_rid = get_world_2d().space
var space_state = PhysicsServer2D.space_get_direct_state(space_rid)
var query = PhysicsRayQueryParameters2D.create(cam_mouse_pos, last_cam_mouse_pos)
var result = space_state.intersect_ray(query)
if result:
print("Detected: ", result.position)
However, I’m not getting any results. I assume it’s either another coordinate mishap (SO ANNOYING), or that the raycast can’t detect Better Terrain’s custom terrain for some ungodly reason. This is just a testing raycast, so I just want to make sure it works, so I want to see if it can detect a hitbox or terrain, both of which I’m getting nothing.
My final goal is to make the raycast check if there’s any empty tiles between the mouse’s last position and its current position and have tiles drawn there.
Also, keep in mind that I am very new to programming.