Get Target Surface on a part, from Touch (Mobile)

I want to get the surface that the player touches (Mobile) and make the name of the surface into a variable.

I haven’t got anything to work, I’ve tried looking thru the creator docs and here on dev forum and haven’t found anything.

I’ve searched thru dev forum as I said, I’ve tried looking thru the docs and of course try to code it myself.

I’ll send a bit of the code if that’s helpful if you reply and help!

To whoever reads this,
Have a good day!

2 Likes

any code would be great! please definitely send any if u have it

1 Like

Alrighty!

        local unitRay = camera:ViewportPointToRay(position.X, position.Y)
        local ray = Ray.new(unitRay.Origin, unitRay.Direction * LENGTH)
        local hitPart, worldPosition = workspace:FindPartOnRay(ray)
-- To get where the touch input is in the world.

 ----------------------------
 local function round(vector,grid)
        return Vector3.new(
            math.floor(vector.X/grid+.25)*grid,
            math.floor(vector.Y/grid+.5)*grid,
            math.floor(vector.Z/grid+.25)*grid
        )
    end
-- I use this to make later make the targeted position snapped into a 2x2 grid.


local ElVecoro = round(Vector3.new(newWorldPosition.X, newWorldPosition.Y, newWorldPosition.Z), 2)
SelectedPart.Position = ElVecoro  

-- then this to set the position

I can provide more if needed!

u could use the ray to find the part and make that a variable id reckon im unsure on how surfaces work so if my suggestion isn’t what you’re looking for that’s okay!

1 Like

how would i use the variable? its alright if u dont got time, anything means tons :slight_smile:

oh i might know, i could maybe get the material of the part and then find out the sides, ill see

maybe! i can look up some stuff too n see

RaycastResults have a normal property, so try using workspace:Raycast instead of workspace:FindPartOnRay. Usage is essentially the same, just that workspace:Raycast has an optional RaycastParams parameter.

1 Like

Thanks, using it now but cant manage to get the surface

By surface, you mean like left, right, top, bottom etc, right? If so, that’s what the normal property is (eg. workspace:Raycast(…).Normal). The thing is, though, that the property is a Vector3 so you’d have to use this to determine the normal.
0,1,0 = top
0, -1, 0 = bottom
-1, 0, 0 = left
1, 0, 0 = right
0, 0, -1 = front
0, 0, 1 = back

If you want a more algorithmic approach you could do something like:

local function getNormalIdFromVector(vector: Vector3) -- vector is the RaycastResult.Normal
    for i, normalId in ipairs(Enum.NormalId:GetEnumItems()) do
        if Vector3.fromNormalId(normalId) == vector then
            return normalId
        end
    end
end

^ That should work, but if you encounter floating point issues (not sure if they are prominent with RaycastResult normals) you can check the magnitude between the two vectors is less than some low number like 0.005 or so.

Unless you mean the position where the ray hits the part, that would be the RaycastResult.Position.

2 Likes

Thanks man! I’ll try rn! Thanks a lot :slight_smile:

1 Like

I get a , "Attempt to index nil with ‘Normal’ " error. Is there any fix?

Yeah, just check that the RaycastResult is not falsy.

So,

local raycastResult = workspace:Raycast(...)
if raycastResult then
    -- do things here
end
-- or use a guard clause
if not raycastResult then
    return
end
1 Like

Works like a charm, thanks man! Means a ton! Have a wonderful day :slight_smile:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.