Godot Version 4.4
So I’ve Followed This Tutorial on how to make mouse aiming in a top down game and added “var query = PhysicsRayQueryParameters3D.create(RayOrigin, RayEnd); var Intersection = SpaceState.intersect_ray(query)
” to it as suggested by the top comment.
However I now get the Error Line 20:There is already a variable named "Intersection" declared in this scope.
Wich is var Intersection = space_state.intersect_ray(rayOrigin, rayEnd)
so I remove the var
from it but then suddenly 5 more errors pop up
Line 18:Identifier "RayOrigin" not declared in the current scope.
Line 18:Identifier "RayEnd" not declared in the current scope.
Line 18:Identifier "SpaceState" not declared in the current scope.
Line 20:Too many arguments for "intersect_ray()" call. Expected at most 1 but received 2.
Line 24:Cannot find member "y" in base "Translation".
Help Please!!
My Code:
extends CharacterBody3D
const SPEED = 5.0
const JUMP_VELOCITY = 4.5
var rayOrigin = Vector3()
var rayEnd = Vector3()
@onready var camera: Camera3D = $Camera
var aim_direction: Vector3 = Vector3.ZERO
func _physics_process(_delta: float) -> void:
var space_state = get_world_3d().direct_space_state
var mouse_position = get_viewport().get_mouse_position()
rayOrigin = camera.project_ray_origin(mouse_position)
rayEnd = rayOrigin + camera.project_ray_normal(mouse_position) * 2000
var query = PhysicsRayQueryParameters3D.create(RayOrigin, RayEnd); var Intersection = SpaceState.intersect_ray(query)
var Intersection = space_state.intersect_ray(rayOrigin, rayEnd)
if not Intersection.is_empty():
var pos = Intersection.position
$Mesh.look_at(Vector3(pos.x, Translation.y, pos.z), Vector3(0, 1, 0))
var input_dir := Input.get_vector("left", "right", "up", "down")
var direction: Vector3 = (transform.basis.x * input_dir.x + transform.basis.z * input_dir.y).normalized()
if direction != Vector3.ZERO:
velocity.x = direction.x * SPEED
velocity.z = direction.z * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
velocity.z = move_toward(velocity.z, 0, SPEED)
move_and_slide()
Seems like this line is two statements mashed together, I belive you want to delete the latter half of it.
1 Like
So Like this
rayOrigin = camera.project_ray_origin(mouse_position)
rayEnd = rayOrigin + camera.project_ray_normal(mouse_position) * 2000
var query = PhysicsRayQueryParameters3D.create(RayOrigin, RayEnd)
var Intersection = SpaceState.intersect_ray(query)
var Intersection = space_state.intersect_ray(rayOrigin, rayEnd)
or Like this?
rayOrigin = camera.project_ray_origin(mouse_position)
rayEnd = rayOrigin + camera.project_ray_normal(mouse_position) * 2000
var query = PhysicsRayQueryParameters3D.create(RayOrigin, RayEnd)
var Intersection = SpaceState.intersect_ray(query)
When I do this
rayOrigin = camera.project_ray_origin(mouse_position)
rayEnd = rayOrigin + camera.project_ray_normal(mouse_position) * 2000
var query = PhysicsRayQueryParameters3D.create(RayOrigin, RayEnd)
var Intersection = SpaceState.intersect_ray(query)
var Intersection = space_state.intersect_ray(rayOrigin, rayEnd)
I get the Errors
Line 21:There is already a variable named "Intersection" declared in this scope.
But when I do this
rayOrigin = camera.project_ray_origin(mouse_position)
rayEnd = rayOrigin + camera.project_ray_normal(mouse_position) * 2000
var query = PhysicsRayQueryParameters3D.create(RayOrigin, RayEnd)
var Intersection = SpaceState.intersect_ray(query)
I get the Errors
Line 18:Identifier "RayOrigin" not declared in the current scope.
Line 18:Identifier "RayEnd" not declared in the current scope.
Line 19:Identifier "SpaceState" not declared in the current scope.
Line 23:Cannot find member "y" in base "Translation".
These errors are very solveable, you don’t have a capital RayOrigin but you do have a rayOrigin variable, so you probably want to use the same capitalization.
Man this is Embarassing, thanks I was able to solve all but one
Line 24:Cannot find member "y" in base "Translation".
The Line in Question
$Mesh.look_at(Vector3(pos.x, Translation.y, pos.z), Vector3(0, 1, 0))
I don’t know how you defined “Translation”, you might be trying to get the y
value of the translator type which is a supposed to be a different language to release your game in, like spanish or french.
I don’t know what exactly you want the mesh to look_at either so I’m not sure wht substitute you want. I would guess replacing Translation.y
with global_position.y
to always look on the same plane as the player, or just use pos
whole sale.
# planar look
$Mesh.look_at(Vector3(pos.x, global_position.y, pos.z), Vector3.UP)
# direct look
$Mesh.look_at(pos, Vector3.UP)
You just made my day, thank you so much man!
Hope you have a great weekend