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

Help Finding The Hit Surface/Axis?

Asked by
Hibobb 40
9 years ago

I need to know on which surface/axis of the part a ray contacted with. Previously I used the surface normal and through some testing found the relationship between the surface and returned normal Vector. However when I tried the same thing with a rotated part, it no longer returns values between -1 and 1. What is the math to find the surface when it a part is rotated? RayCast is a basic raycasting function that returns that part hit, position of the hit, and the surface normal.

local hit,position,norm = RayCast(startPos,direction,tool.Parent)
if hit then
    wait()
    print(norm)
    local x,y,z = norm.X, norm.Y, norm.Z
    if x ~= 0 and (y == 0 and z == 0) then
        hitx = true
    elseif y ~= 0 and (x == 0 and z == 0) then
        hity = true
    elseif z ~= 0 and (x == 0 and y == 0) then
        hitz = true
    end
end

1 answer

Log in to vote
1
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
9 years ago

Since you're given a unit vector for a rotated part, it's easier to convert the given Vector3 to object coordinates. Then, your original comparison works fine:

local hit, position, norm = RayCast(startPos, direction, tool.Parent)

if hit then
    wait()
    print(norm)
    norm = hit.CFrame:vectorToObjectSpace(norm) --This is where the magic happens.
    local x,y,z = norm.X, norm.Y, norm.Z
    if x ~= 0 and (y == 0 and z == 0) then
        hitx = true
    if x > 0 then
        print("Front")
    else
        print("Back")
    end
    elseif y ~= 0 and (x == 0 and z == 0) then
        hity = true
    if y > 0 then
        print("Top")
    else
        print("Bottom")
    end
    elseif z ~= 0 and (x == 0 and y == 0) then
        hitz = true
    if z > 0 then
        print("Left") --These two might be backwards!
    else
        print("Right")
    end
    end
end
0
Wait so what did that do exactly? Hibobb 40 — 9y
0
If you look at the methods section of the CFrame page on the wiki, you'll get their explanation (as well as all the rest of the methods.) Basically, the unit vector is rotated as if the part itself is *not* rotated at all. adark 5487 — 9y
0
Oh nice! Thanks for the help Hibobb 40 — 9y
Ad

Answer this question