Positioning part related to other part and mouse


#1

I started to script a plugin for a game I’m working on, but I ran into a problem with one of the features. I’m trying to create a part at the position of the mouse but the part is aligned with the middle of the object the mouse has targeted. (look at the picture below for a better explanation)

So basically the part is in the middle of the object but can be moved in the other direction (the red line)
I got it to work as long as the targeted object is on a straight line with the grid. If the object is rotated for example 45° the part just moves further in a straight line and isn’t in the middle of the object anymore.

I know this is kind of a bit complicated but if someone just could tell me in what direction I should look or what I should use to solve this, it would help a lot!


#2

You can use a Ray and use the ClosestPoint method, when you select the part, create the Ray from beginning to the end and use that method to get the closest point on the ray related to the mouse.

local PartRay=Ray.new(Part.CFrame*CFrame.new(0,0,Part.Size.Z/2),Part.CFrame.lookVector)

local ClosestPoint=PartRay:ClosestPoint(Mouse.Hit.p)


#3

Okay thanks will try that out.


#4

It worked only you forgot to take the position of Part.CFrame * CFrame.new(0,0,Part.Size.Z/2)


#5

My bad lol
Glad it worked.