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

How do I make it so I can detect what side of the mouse a brick is touching?

Asked by
mxpvjn 75
8 years ago

I need to know how I can detect which surface of a part the mouse is on.

1 answer

Log in to vote
6
Answered by 8 years ago

First of all, think basic. There is a position a part has, and there is a position Mouse has. In any other conditions, if X's absolute value is bigger than Y and Z's absolute values, it means Mouse is at right surface or left surface of part. Then, if X is negative, it's left, if X is positive, it's right. So if there is non-rotated part and a Mouse touching to the part:

Surface = nil
if math.abs(X) > math.abs(Y) and math.abs(X) > math.abs(Z) then
    if X < 0 then
        Surface = "Left"
    else
        Surface = "Right"
    end
elseif math.abs(Y) > math.abs(X) and math.abs(Y) > math.abs(Z) then
    if Y < 0 then
        Surface = "Bottom"
    else
        Surface = "Top"
    end
elseif math.abs(Z) > math.abs(X) and math.abs(Z) > math.abs(Y) then
    if Z < 0 then
        Surface = "Front"
    else
        Surface = "Back"
    end
end

Now, let's say part is rotated. It looks much harder now, right? Yep, it is. But we can cheat to make it easier :)

Let's say there is a rotated part, so we will need basic CFrame info, and some other basic info about model's primary part speciality.

model = Instance.new("Model")
model.Name = "SurfaceFinder"
MP = Player.Character.Humanoid.TargetPoint      --Mouse's Position
PP = Part.Position                                  --Part's Position

Part1 = Part:Clone()
Part1.Parent = model
Part2 = Instance.new("Part")
Part2.Position = MP

model.PrimaryPart = Part1
model:SetPrimaryPartCFrame(CFrame.new(PP.X,PP,Y,PP,Z)*CFrame.Angles(0,0,0))

After doing this, you need to use which surface of Part2 touching at Part1

IMPORTANT NOTE: You have to change the script at first code block to make it work with part's size, otherwise it will tell you wrong surface of the part.

0
If it helps, would you mind to give a rep? :) superalp1111 662 — 8y
0
ok mxpvjn 75 — 5y
Ad

Answer this question