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

How can I identify the side of an object the mouse's click was?

Asked by 8 years ago

I need get the distance from the center of an object to the mouse click position, I can, but if I go to the other side of the object the value turns negative, and I need identify if the click was at left or right of the object's center, so I cant use the value signal to do this, I hope you can help me: image

I'm using this to get the value

mouse.Hit:toObjectSpace(object.CFrame).p.X

1 answer

Log in to vote
0
Answered by
Link150 1355 Badge of Merit Moderation Voter
8 years ago

You can use the math function math.abs() to construct a new Vector3 from the absolute value of each components of the p vector and then retrieve it's X component:

local tool = script.Parent


local function onButton1Down(mouse)
    local part = mouse.Target

    -- Just to make sure the mouse isn't pointing nowhere or at the baseplate
    if part and part.Locked == false then
        local vec = mouse.Hit:toObjectSpace(part.CFrame).p

        print(Vector3.new(math.abs(vec.x), math.abs(vec.y), math.abs(vec.z)).X)
    end
end

local function onEquipped(mouse)
    mouse.Button1Down:connect(function() onButton1Down(mouse) end)
end


tool.Equipped:connect(onEquipped)

Hope this helps. If it did, please leave an upvote and mark my question as answered. :) If you need anything else, leave me a message.

0
Thanks for your answer, but this does'nt solved.. please look the image link, let me try explain in another way my english is not so good...I couldn't figure out what is going on the Part's local space, if I click on it after 2 studs from the its center/origin, the X axis says 2.0 but if I click on the same point again but with my character on its other side it says -2, like the X axis has inverte TheDarkeen 0 — 8y
0
Oh, I see. I'm sorry, I thought you meant it turned negative if you clicked at the other side of part on the same axis, if it makes sense. I've been trying to figure out how to fix it for two hours now, but still nothing. I'm not particularly good with Vector and CFrame math. :P Link150 1355 — 8y
Ad

Answer this question