Let's see what this dumb bot spews out.
How do I code a 3D unit selection box for Godot 4?
Thank you for your interest! This bot doesn't have anything interesting to say at the moment, but please check back soon for new updates!
- Edited
Oh fuck you, I knew I'd break this thing in 5 seconds.
earlier I saw it reply to a post about how to do something in godot, marked as godot 4 programming help, with "it depends on what engine you're using and what you're trying to do." on a related note, I'm seeing a lot of people fall victim to the expert effect but with AI. they just assume everything it says is true because they don't know better but anyone who knows even the most basic things about it can tell them it's wrong at a simple glance.
I don't know much about 3D so I can't give a real answer at the moment but if no one else does within a couple days I'll finagle something together just so people finding this through the search box in the future don't get accidentally pranked
- Edited
I asked ChatGPT the same question, and it gave this answer. I don't know if it's correct:
In Godot 4, the process of creating a 3D unit selection box involves a combination of scripting and utilizing the engine's built-in features. Here's a step-by-step guide to help you code a 3D unit selection box in Godot 4:
Step 1: Setting up the scene
Create a new scene and add a 3D node as the root of the scene.
Add a MeshInstance or Spatial node to represent your selectable units. Ensure that the units have a collider, such as a CollisionShape or CollisionPolygon, attached to them.
Step 2: Implementing the selection box
Create a new script by attaching it to the root node of your scene.
In the script, define a variable to store the initial mouse position when the selection box starts:
var selectionStart: Vector2
Override the _input function to handle mouse input:
func _input(event: InputEvent) -> void:
if event is InputEventMouseButton:
var mouseEvent = event as InputEventMouseButton
if mouseEvent.button_index == BUTTON_LEFT and mouseEvent.pressed:
# Store the initial mouse position
selectionStart = mouseEvent.position
# Start drawing the selection box
start_selection_box()
elif mouseEvent.button_index == BUTTON_LEFT and not mouseEvent.pressed:
# Finish drawing the selection box
end_selection_box()
Create the start_selection_box function to initialize the selection box:
func start_selection_box() -> void:
# Create a SelectionBox node
var selectionBox = SelectionBox.new()
# Add the SelectionBox node to the scene
add_child(selectionBox)
# Set the initial position of the selection box
selectionBox.position = selectionStart
# Store the SelectionBox reference in a variable
$SelectionBox = selectionBox
Implement the end_selection_box function to finalize the selection box and select the units within it:
func end_selection_box() -> void:
# Get the final mouse position
var selectionEnd = get_viewport().get_mouse_position()
# Calculate the selection box rectangle
var selectionRect = Rect2(selectionStart, selectionEnd - selectionStart)
# Iterate over the units in the scene
for unit in $MeshInstance.get_overlapping_bodies():
# Check if the unit's position is inside the selection box
if selectionRect.has_point(unit.global_transform.origin):
# Perform unit selection logic
unit.selected = true
Now, create a new script for the SelectionBox node. In this script, define a draw function to draw the selection box:
extends Node2D
func draw() -> void:
draw_rect(Rect2(Vector2(), get_viewport().get_mouse_position() - position), Color(1, 1, 1, 0.3), true)
Attach the script to the SelectionBox node.
Step 3: Testing the selection box
Instantiate your scene in a scene or as the main scene.
Run the project and test the selection box by clicking and dragging the left mouse button. The units intersecting with the selection box should be selected.
That's it! You have now implemented a basic 3D unit selection box in Godot 4. Feel free to customize and enhance the code according to your specific requirements.
DaveTheCoder This is hilarious.
Chat bots just piss me off so much lol.