How do I make a part rotate relative to a point?

I have created a script which creates a part at the location of the mouse, It currently just appears as it is globally because I have only manipulated its position. How can I make it so that the part is always facing left relative to the vector of the humanoid root part to mouse position? I also want to know how to do it in a way where the brick is not rotated on any other axis, just the Y I believe. For example if the mouse position is higher than character, I still want the part to be parallel with the floor but still rotated left 90 degrees.

2 Likes

do you want the part to be facing at the mouse ?

1 Like

The part will spawn at the mouse, if the direction from the character to the mouse is forwards I want the part to be rotated 90 degrees

1 Like

The mouse’s “direction” depends on the surface it hits so if it hits a surface facing to the right the mouse.Hit will be rotated to the right, is that the rotation you want?

1 Like

Im using mouse.hit.position so that it just appears at its place and Im trying to use the vector between the character and the mouse position to create a direction to orient the part but I just do not know how to do it


This is a picture of what I want to happen, so regardless of where the part is spawned its always facing left from the player.

1 Like

You would place the position at mouse.Hit.Position and set the rotation of the Y axis to be: (humanoidRootPart.CFrame.Position - mouse.Hit.Position).Unit.Y + 90, make sure to use math.rad() on that.

local angle = (humanoidRootPart.CFrame.Position - mouse.Hit.Position).Unit.Y + 90
part.CFrame = CFrame.new(mouse.Hit.Position) * CFrame.Angles(0, math.rad(angle), 0)

Something like this should work?


thanks for the diagram.

1 Like

Its just rotated itself 90 degrees overall but is still not reacting to any changes in orientation from the player, It will just consistently spawn in the same direction just rotated 90 degrees after I tried what you gave me.

I was calculation the angle incorrectly, ig what about this maybe…?

local direction = (mouse.Hit.Position - humanoidRootPart.CFrame.Position).Unit
local angle = math.deg(math.atan2(direction.X, direction.Z)) + 90 -- Unsure about this but should theoretically work lol

part.CFrame = CFrame.new(mouse.Hit.Position) * CFrame.Angles(0, math.rad(angle), 0)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.