@Thortilla said:
@TwistedTwigleg said:
This is the code that projects a Ray from the mouse coordinates
var ray_lenght = 1000
var mouse_pos = get_viewport().get_mouse_pos()
var camera = get_node("camera")
var from = camera.project_ray_origin(mouse_pos)
var to = from + camera.project_ray_normal(mouse_pos) * ray_length
Then you use those coordinates to raycast using intersect_ray in the direct space state.
Something like this:
var space_state = get_world().get_direct_space_state()
# use global coordinates, not local to node
var result = space_state.intersect_ray( from, to )
Then you can do whatever you need with the results of the raycast.
Jajajajaja I dont know if some of you still be here, I uses this code above and it works really great, thank you very much!, but now I need to proyect the raycast that only impact in one determinated object (in my case the flour), I dont know if you can understand me well because english is not my first language. In other words, I need to make that this part of the code:
var result = space_state.intersect_ray( from, to )
give me where the ray is coliding with the flour (without taking in account that there are more objects at the front.
Thanks!
I think most of us are still here, doing stuff here and there :smile:
I’m glad the code worked. It is from the documentation on raycasting, which is where I learned about it initially.
To answer your question, I would suggest putting all of the objects you want your raycast to collide with, like your floor, either on a separate collision layer, or have the objects on multiple collision layers.
Both in DirectSpace and DirectSpace2D, the intersect_ray function can take a list of collision layers as an argument. Using this, you can make it where the raycast only collides with objects on specific layers. Using this, you can make it where you can control what a raycast can collide with by changing the collision layer(s) they are on.
You’ll just need to set the objects to whatever collision layer(s) you want, figure out what the Bitwise value is for the layer(s) you need to collide against, and then pass that to the intersect_ray
function.
In this QA post, KidsCanCode explains how to convert from a collision layer to a integer value.
Once you have the integer value for the collision layer, you just need to pass it in to intersect_ray
. Something like this (untested):
var result = space_state.intersect_ray(from, to, self, [collision_mask])
And if you want to check against multiple layers, then you just need to add more collision masks to the list:
var result = space_state.intersect_ray(from, to, self, [collision_mask, collision_mask_two, collision_mask_three])
Hopefully this helps! :smile: