Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I find the highest surface on a specific x and z coordinate pair?

Asked by 6 years ago

Lets say I have two variables (x and z), in which are some X and Z coordinates. What I want to find out at what Y coordinate is the highest surface (terrain or part). My only idea was to create an invisible, super tall tube which is positioned at the X and Z coordinates, then create a small, invisible cube at the top of the tube, let it drop down, and write down the cube's Y coordinate. I know this method is ineffective, so i's asking for a better way. I heard that you could do something with "Rays" or something, but I'm not sure. Any help will be appreciated.

0
You would want to use raycasting http://wiki.roblox.com/index.php/Raycasting I also have a place on raycasting if it helps. User#5423 17 — 6y
0
Ok thank you. I read the tutorial, but I still really don't get how to : 1. Position the ray 2: Find out the point at which it hit RiskoZoSlovenska 378 — 6y

1 answer

Log in to vote
0
Answered by
RayCurse 1518 Moderation Voter
6 years ago
Edited 6 years ago

Positioning the Ray

First, you need to position the ray at the x and z coordinates specified along with a y at 0. The ray needs to be pointing directly upwards. With all this, this is how the ray would be constructed:

local ray = Ray.New(Vector3.new(x , 0 , z) , Vector3.new(0 , 1 , 0) * 999)

Intersection

Next, you need to find where the ray intersects with the part if it does at all. While the ray itself doesn't have method for this, the workspace does. For example, you would do this to find the part the ray intersects with:

local ray = Ray.New(Vector3.new(x , 0 , z) , Vector3.new(0 , 1 , 0) * 999)
local part , intersection = workspace:FindPartOnRay(ray)

Note that the FindPartOnRay method returns the part that was intersected and the position of intersection. The method has additional return values and parameters that you can find here, but these aren't needed here. If the ray does not intersect with a part, then the part is nil.

Now all we need to do to get the y coordinate is this:

local ray = Ray.New(Vector3.new(x , 0 , z) , Vector3.new(0 , 1 , 0) * 999)
local part , intersection = workspace:FindPartOnRay(ray)
local yCoordinate = part.Position.Y

Keep in mind that rays can be expensive so you shouldn't use them excessively.

0
Thank you, I believe this will solve my problem. Also, a couple of questions: Does this also work with terrain? If I want the highest position on certain x and z coordinates, shouldn't the start of the ray be positioned as high up as possible and pointing down? Lastly, I see you save the Y position of 'part'. Isn't 'part' the Part that it hit, or is it the point? I am totally new to raycasting. RiskoZoSlovenska 378 — 6y
0
I am pretty sure that the ray will intersect with terrain. The part that is returned is the part itself not the position. The intersection is where the ray hit the part. Remember, a ray is a line that starts at one point and goes infinitely in one direction. In this case, the ray goes infinitely upwards so there is no need to position the origin of the ray on an arbitrarily high y position. RayCurse 1518 — 6y
0
I'm just started using this after a long time, and your answer helped. HOWEVER, your method finds the LOWEST y position on the specified XY coordinates. Also, rays, while they are techically infinite, have a range limit of 5000 studs, so they are not exactly infinite. So what I did is that I positioned the ray 3500 studs high, and made it point DOWNwards. Now it works perfectly, thank you! RiskoZoSlovenska 378 — 5y
0
This was seven months ago wow lol. But your welcome :) RayCurse 1518 — 5y
Ad

Answer this question