Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Figuring out how to do Orientation and Position with PrimaryPartCFrame?

So basically what I'm trying to do is have a part that follows the mouse, while maintaining a specific orientation as well as aligning with the players cursor.

This is what I have for that so far:

local model = workspace.Model
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

model:SetPrimaryPartCFrame(CFrame.new(mouse.Hit.X - 3, 1.105, mouse.Hit.Z))

The above sets the position but I cant for the life of my figure out how to do orientation as well as the position. If anyone could help me out I'd be grateful!

2 comments

0

^ Leaves me with another question of how do I keep its Y value at 1.105?

1 answer

✓ Accepted answer

You can translate a CFrame in world space by adding or subtracting a Vector3 value. This method will also keep the rotation of the coordinate frame intact.

local model = workspace.Model
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

local targetCF = model.CFrame + Vector3.new(mouse.Hit.p - model.Position)
model:SetPrimaryPartCFrame(targetCF)

If you want to lock the y axis of the CFrame, instead of adding the whole Vector3, just add the individual x and z components of the Vector3. In addition if you want to make the model actively follow the mouse instead of just setting the position once, you can utilize the Move() event of the mouse.

local model = workspace.Model
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

mouse.Move:Connect(function()
	local distanceVector = mouse.Hit.p - model.Position
	local translateVector = Vector3.new(distanceVector.X , 0 , distanceVector.Z)
	local targetCF = model.CFrame + translateVector
	model:SetPrimaryPartCFrame(targetCF)
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.