How can i use RayCastLocal to find Y position

  1. What do you want to achieve? My goal is to input the X and Z vectors from the real world into a function, and have the function return the Y position of the vertex or triangle that is at the same or a close X or Z position, or if using RaycastLocal, just the Y position.

  2. What is the issue? I’m using RaycastLocal, as the normal Raycast does not work with EditableMesh (due to Beta Features and Hitbox not updating). When I use RaycastLocal, for some reason, it only prints 0,0
    image
    and I don’t know what to do with this value.

  3. What solutions have you tried so far? I have tried to find more information about RaycastLocal because I don’t understand it well, but no one seems to be discussing it at the moment.

My goal is to return a Y value based on how high the MeshPart or vertex is at the position provided (X, Z).

-- HitBox that works for normal RayCast
--local origin = Vector3.new(-273.75, 10, -83.25)
--local direction = Vector3.new(-273.75, -10, -83.25)

-- Hitbox that does not work for RayCast
local origin = Vector3.new(-310.75, 6, -132.25)
local direction = Vector3.new(-310.75, -6, -132.25)

-- Making RayCastLocal
local point, triangleID, barycentricCoords = Variables.EditableMesh:RaycastLocal(origin, direction)

local function castRayFromVectors(origin, direction)
	if not Variables.EditableMesh then return end

	-- Convert the provided world-space origin and direction to object space
	local relativeOrigin = Variables.Object.CFrame:PointToObjectSpace(origin)
	local relativeDirection = Variables.Object.CFrame:VectorToObjectSpace(direction - origin).Unit

	-- Perform the raycast in local space
	local triangleId, point, barycentricCoordinate
	triangleId, point, barycentricCoordinate = Variables.EditableMesh:RaycastLocal(relativeOrigin, relativeDirection * 100)

	if not triangleId then
		-- Didn't hit any triangles
		print("No Hit Triangles")
		return
	end

	-- Interpolate UVs within the triangle
	local vert1, vert2, vert3 = Variables.EditableMesh:GetTriangleVertices(triangleId)

	local uv0 = Variables.EditableMesh:GetUV(vert1)
	local uv1 = Variables.EditableMesh:GetUV(vert2)
	local uv2 = Variables.EditableMesh:GetUV(vert3)

	local u = (barycentricCoordinate.x * uv0.x) + (barycentricCoordinate.y * uv1.x) + (barycentricCoordinate.z * uv2.x)
	local v = (barycentricCoordinate.x * uv0.y) + (barycentricCoordinate.y * uv1.y) + (barycentricCoordinate.z * uv2.y)

	return Vector2.new(u, v)
end

local uv = castRayFromVectors(origin, direction)
if uv then
	print("UV:", uv)
else
	print("No intersections found.")
end

By the way, you’re seeing two sets of variables for origin and direction. The one that is just a markup is the old hitbox of the mesh that works for Raycast, but I want the new updated hitbox so I can get the X, Y, Z vector that the raycast is supposed to detect as it passes through the MeshPart.

I think I’m close to making it work. Thanks to anyone who is helping me with this.

Pls help no other devforum is talking about RayCastLocal ;(

Pwease anyone ;( or idk maybe my post bugged and i need repost, lol im talking alone

You need to be patient; it’ll usually take at least a few hours for someone to try and help.

1 Like

oki :<
i think my post was patched for some how

Why are you multiplying your coordinates by the Texture UVs? Are you sure the UVs themselves are not just 0? What do you want this code to do, why is it taking UV mapping into account?

oh the code is just a test i forgot to remove the uv to my newest code, and when i was doing the code the energy drop down and i lose it, but uv map is just a test it have no sense about what im trying to do. but imagine that the function is a module script, and have a script that will call that function from the module, so it will call the function and send the cordinates x and z, and the function is supose to use raycastlocal and put the same x and z as the origin position and direction of the raycastlocal, and for the y origin just put 10 and -10 so the ray can go through the meshpart, and then the first intersection of the ray if meshpart need to get the y position and send back to the script that call the function and the script will put the y position to the part.

sorry for long text