-
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.
-
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
and I don’t know what to do with this value. -
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.