Reputation: 13
I am making an RTS game in godot4 4.2.1 Stable, just for shiggles. I am right clicking to perform a raycast. When I start the game and I right click, I am getting a duplicate input and it is giving me 2 seperate answers. Here is the code I am working with and its output.
if Input.is_action_just_pressed("Right_Click"):
rayCast()
if results.size() > 0 && ControlsMaster.selected_actors.size() > 0:
print("RIGHT CLICKED at: ", round(results.position), "\n")
ControlsMaster.mouse_pos_ref = round(results.position)
ControlsMaster.formation("square", results)
print("complete\n")
results.clear()
func rayCast():
var worldspace = get_world_3d().direct_space_state
var from = project_ray_origin(mouse_POS)
var to = project_position(mouse_POS, 1000)
results = worldspace.intersect_ray(PhysicsRayQueryParameters3D.create(from, to))
The raycast is coming from the mouse and projecting to the map perpendicular to the screen. The ouput that I get is the following. This output is coming from a single click.
selected_actors array contains: [UNIT_0001:<CharacterBody3D#36691772857>, UNIT_0011:<CharacterBody3D#38033950217>]
RIGHT CLICKED at: (-40, 8, -6)
lead_actor_position(-40.12659, 7.526592, -6.26702)
result position(-40.12659, 7.526592, -6.26702)
{ "Position_0": (-40.12659, 7.526592, -6.26702), "Position_1": (-45.12659, 0, -6.26702), "Position_2": (-35.12659, 0, -6.26702), "Position_3": (-40.12659, 0, -1.26702), "Position_4": (-40.12659, 0, -11.26702), "Position_5": (-45.12659, 0, -1.26702), "Position_6": (-35.12659, 0, -1.26702), "Position_7": (-45.12659, 0, -11.26702), "Position_8": (-35.12659, 0, -1.26702) }
complete
RIGHT CLICKED at: (-78, 0, -107)
lead_actor_position(-78.43892, 0, -106.6246)
result position(-78.43892, 0, -106.6246)
{ "Position_0": (-78.43892, 0, -106.6246), "Position_1": (-83.43892, 0, -106.6246), "Position_2": (-73.43892, 0, -106.6246), "Position_3": (-78.43892, 0, -101.6246), "Position_4": (-78.43892, 0, -111.6246), "Position_5": (-83.43892, 0, -101.6246), "Position_6": (-73.43892, 0, -101.6246), "Position_7": (-83.43892, 0, -111.6246), "Position_8": (-73.43892, 0, -101.6246) }
complete
Im not sure how to add the 2 scripts that would make up the context, I dont come here often. Sorry in advance.
I have tried changing it to a key press or press and hold and it still does the double output.
The closer the raycast is to the center of the camera, the more accurate it is. The farther it is from 0,0,0 the further the actor spawns from where I actually did the mouse press at.
I have also tried to condition it so that it would check a variable called was_mouse_pressed. If it was false, it would allow the click, if True, then it would not allow the click. When I did this, it would run through the code, set it to True and then run the code again with it being false at the start and then turning True again. Even though I never had a code line to reset the bool to false.
I really don't understand and might switch to Unreal for the project. Unity isn't an option for me.
Upvotes: 0
Views: 230
Reputation: 40315
There is nothing in your question to tell me when is this code snippet running:
if Input.is_action_just_pressed("Right_Click"):
rayCast()
So I do not know if this might be in a method that is being called twice for some unknown reason.
Now, assuming you would have catched something like that. This is what comes to mind: there are two instances with this script in runtime, so they both do a raycast and they both get a result. You might be able to confirm if this is the case with print(self)
.
The code suggest the script is intended to be attached to a Camera3D
. If there are multiple Camera3D
in the scene that have this script attached but are placed at different positions, it makes sense that they get different results from their raycats.
Do you have multiple Camera3D
in the scene with this this script attached? You might want to only do this in the current
one.
Alternatively, you might write this code in a central place and use get_viewport().get_camera_3d()
to get the current camera, and use it to do the projection for the raycast.
Upvotes: 0