Skip to content

Instantly share code, notes, and snippets.

@lamarmarshall
Created March 2, 2025 12:59
Show Gist options
  • Save lamarmarshall/20d48fe8f37482865ae5e6dab77aaf7e to your computer and use it in GitHub Desktop.
Save lamarmarshall/20d48fe8f37482865ae5e6dab77aaf7e to your computer and use it in GitHub Desktop.
godot, raycast
extends Camera3D
@onready var ray_cast_3d: RayCast3D = $RayCast3D
@export var grid_map: GridMap
@export var turret_manager: Node3D
@export var turret_cost := 100
@onready var bank := get_tree().get_first_node_in_group("Bank")
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
var mouse_position: Vector2 = get_viewport().get_mouse_position()
ray_cast_3d.target_position = project_local_ray_normal(mouse_position) * 100.0
ray_cast_3d.force_raycast_update()
if ray_cast_3d.is_colliding():
if bank.gold >= turret_cost:
var collider = ray_cast_3d.get_collider()
if collider is GridMap:
var collision_point = ray_cast_3d.get_collision_point()
var cell = grid_map.local_to_map(collision_point)
Input.set_default_cursor_shape(Input.CURSOR_POINTING_HAND)
#make sure gride cel isst to item 0
if Input.is_action_just_pressed("click"):
if grid_map.get_cell_item(cell) == 0:
grid_map.set_cell_item(cell, 1)
var tile_position = grid_map.map_to_local(cell)
turret_manager.build_turret(tile_position)
bank.gold -= turret_cost
else:
Input.set_default_cursor_shape(Input.CURSOR_ARROW)
else:
Input.set_default_cursor_shape(Input.CURSOR_ARROW)
#printt(ray_cast_3d.get_collider(), ray_cast_3d.get_collision_point())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment