I’m currently trying to make it so when you click a part, it’ll create an attachment at that position. This is my code and it works find when the part is not rotated.
local Attachment = Instance.new("Attachment", Mouse.Target)
local RelativePosition = Mouse.Hit.p - Mouse.Target.Position
Attachment.Position = RelativePosition
Attachment.Visible = true
Here’s a gif that shows the problem, the first part has the default orientation while the second part has a random one.
The solution is RelativePosition = Mouse.Target.CFrame:inverse() * Mouse.Hit.p, where the mouse position is being translated into the relative coordinate space of the target’s CFrame.
Though one problem I’ve just come up with is that I also need the Attachment to rotate so that it’s facing where the mouse clicked. Not sure how I’d go about this
That is also not how mouse CFrame works. You need to cast your own ray to find the surface normals. Mouse hit CFrame is oriented the same way that the camera is. Essentially, after you have casted the ray, the CFrame would need to be
That does not make any sense, rotation is not the same thing as position (you gave no base). Also if you were to base it off of object center, the particle orientation would be incredibly inaccurate on objects that are not perfect spheres.
I’ve already sorted setting the position of the attachments, but I came up with another problem where I needed the attachment to also face where I click.
Shouldnt it be facing away from the surface you clicked?
aka in direction of the surface normal
my example would (after casting the appropriate ray) put the attachment at where you clicked, facing away from the surface, and relative to the part so it is correctly positioned.
UnitRay might be helpful in getting origin and direction so you can cast your own N-length ray that ignores or whitelists any specific objects if need be.
Hi! As I was reading this, I noticed the solution is a bit complex, especially for those who may stumble on this in the future.
If you want something which will place an attachment where you click on a part, you can use mouse.Hit, mouse.Target, and attachment.WorldCFrame.
Example:
local function inputBegan(input, textBoxFocused)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
local mouse = game:GetService("Players").LocalPlayer:GetMouse()
local target, hit = mouse.Target, mouse.Hit
if target then
local New_Attachment = Instance.new("Attachment", target)
New_Attachment.WorldCFrame = hit
New_Attachment.Visible = true
end
end
end
game:GetService("UserInputService").InputBegan:Connect(inputBegan)