Raycasting help needed

Hi, I have been trying to use raycast to predict the path between 2 parts when it has another part obstructing the path.
For example: Imagine that I want to throw a ball and I want that ball to land on the red part, what I want to do is make the ball land on the red part by throwing it from the green part.
I would appreciate any kind of help!

2 Likes

I’m pretty sure that with raycasting, it only creates an infinite ray in both directions parallel to the middle point.

A Ray is a half-line, finite in one direction but infinite in the other. It can be defined by a 3D point, where the line originates from, and a direction vector, which is the direction it goes in.

You’d either need to create multiple points to project rays from, but that would just be inefficient. There are better ways to do this, like using Linear Interpolation to get the distance and then use some form of trajectory to go a certain few studs over the obstacle whenever a ray is hit using workspace:Raycast() which will return any obstacle hit and it’s data within a RaycastResult datatype, because Ray.new() and FindPartOnRay are both now deprecated.

1 Like

What is " Linear Interpolation"? I’ve never heared of it before.

1 Like

Linear Interpolation (scroll down to find the documentation), short for the popular term “Lerp” is when you specify a goal and a fraction (0.1 - 1 aka 1 - 10) and it returns the CFrame distance between the first object and the fraction (in rlua).
In this case I suppose you could calculate the fraction between the first part and the obstacle? Though maybe this too is too much of a verbose solution and there’s probably a better way.

1 Like

i have an idea: this will make the part always go above the topmost face of the middle part, regardless of the geometry:
What it does is it creates a ray that scans upward until it finds an opening, determining where the top is for the parts in between (the blue part in your picture)
It returns the positions of both corners of that part

local startPart = workspace.GreenPart
local endPart = workspace.RedPart

local deltaYFidelity = 0.1 -- how fine each ray goes up
local lastheight = 0;

function calculateRay()
     local params = RaycastParams.new()
      params.FilterType = Enum.RaycastFilterType.Blacklist
      params.FilterDescendantsInstances = {startPart}

     lastheight += deltaYFidelity
     local vector = endPart.Position - startPart.Position
     local ray = workspace:Raycast(startPart.Position + Vector3.new(0, lastheight + 0), vector.Unit * Vector3.new(1, 0, 1) * vector.Magnitude, params)
     
     if ray and ray.Instance and ray.Instance ~= endPart then
         return "fail" -- meaning there is a part in between
     else
         return "found"
     end
end

repeat
     local status = calculateRay()
until status == "found"

-- lastheight is the height at which the top of the huge blue part (in your picture) would end, letting you know where it is safe to cross over
local firstcornerPosition = startPart.Position + Vector3.new(0, lastheight, 0)
local endcornerPosition = endPart.Position + Vector3.new(0, lastheight, 0)
-- gives you the positions of the corners you have to navigate around
2 Likes

The ray cast works! Now, how would I make a part to follow that raycasting? I know how to use body velocity and stuff so don’t worry, I just need to know how to make it follow the raycast.

Actually, nvm. I figured out how to make it, thanks you both!

1 Like