Raycast between 2 attachments

Hiwi! Thank you for reading! Im stuck…

Im trying to raycast using 2 attachment positions. Supposedly raycast is asking for 2 vector3.

But. If I supply a position for the second argument, isnt working good. If I (goal - origin).Unit*X. Im getting a little undesired offset.

Whats a good approach to raycast from point to point, in this case Im using some attachments (which is not important, its just two vector3 got from the attachments world coordinates)

can you show your current code?

1 Like

Are you making sure to use the attachments World position instead of .Position?

2 Likes

Right now my script is a total mess, cause I made lots of experiments… @firsttobebear
So I will write a quick example code, just to show what I want, and Im not getting :v
(raycast seems very simple to use, but I cant make it work between 2 attachments…)

local r1 = game.Workspace.ATT1 -- imagine these both are attachments
local r2 = game.Workspace.ATT2

while true do
ray = workspace.Raycast(r1.WorldPosition, r2.WorldPosition)
print(ray.Instance)
wait(2)
end

I tried different ways to do that simple thing, and the best I got is a little offset of the ray, like 3 studs offset, from the attachment positions.

I just want to see an easy example. The best working thing its just r2.WorldPosition - r1.WorldPosition)Unit*X. But Im having a little offset…

well hold on. maybe I didnt tried worldPosition for both attachments… But, can u just show a little quick dummy example? If I provide 2 vector3 positions based on world coordinates should work? idk why mine is not working…

Isn’t it suppose to be Workspace:Raycast() and you don’t give those arguements, it should be origin, direction and RaycastParams.

1 Like

I know that. If I give a look vector, everything works nicely, but, I need to give a vector3 position comming from an attachment c:

Oh yes, and thats a dummy example, obviously doesnt work…

Idk much about VectorMath as It hasn’t came in my studies but I think Dot product is what you need.

1 Like

Like you said, the raycast needs two vector3s given to it for it to work. One of these is the position of the start of the ray and the other is the direction and magnitude of the ray. If you want a ray to cast from one point, P1 to another point, P2 then you’d do:

workspace:Raycast(P1.Position, P2.Position - P1.Position)

edit: for attachments as this is, these should be P1.WorldPosition, P2.WorldPosition as has been pointed out in the thread already

5 Likes

Thank you everyone! @dthecoolest @McThor2 @Beastcraft_Gaming @firsttobebear

I made a new baseplate, and test it all, and its working great… The problem was not the raycast… it was just the mess I was doing. Thank you so much! I was the silly one here by doing a common mistake… xD

idk which post to mark as the solution cause all of you were right :3

1 Like