|Topic was automatically imported from the old Question2Answer platform.
I’m working on a project similar to XCom: UFO Defense. My current problem is that I can’t get the specific mesh the mouse selects when using;
#get current physics state
var space_state = get_world().direct_space_state
#get current mouse position in the viewport
var mouse_position = get_viewport().get_mouse_position()
#set the ray origin
RayOrigin = project_ray_origin(mouse_position)
#set the ray end point
RayEnd = RayOrigin + project_ray_normal(mouse_position) * 2000
#get the ray hit
var intersection = space_state.intersect_ray(RayOrigin, RayEnd)
if not intersection.empty(): #if there is a proper ray hit get it's position
mouse_position_3d = intersection.position
What returns is the GridMap object. I know I could use the coordinates provided to guess the cell, however my tree is split into separate GridMaps.
The Ray is also only returning the GroundMap, even when it should be intersecting meshes other than a ground mesh first. I.E. Trees and walls.
My intention is to prevent the player from clicking on a wall or obstacle and causing the character to move. The other important thing is that when a character shoots, I want to detect the intersecting mesh properly so it can potentially destroy obstacles. At the moment I can’t even get the proper GridMap detected.
All currently used mesh instances have static bodies with collision shapes.
I’ve noticed the ray will detect the GroundMap even when clicking on on a cell that doesn’t have a mesh in it.
Is the Y position of where you’re casting from what you think it should be?
I haven’t worked much in 3D but a naive approach would be to cast multiple rays with different collisions layers then determine your target based on the smallest distance of those rays.
timothybrentwood | 2021-05-04 21:16