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.
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)
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.