Align something to a parts axis

Hi everyone!

I am having a problems with forcing a part to follow the mouse on a parts axis (The part is rotated). This video here explains it:

How can I achieve this?

1 Like

Can you show the code you are using to choose the position?

The problem is that I don’t know how I can implement anything needed to solve this situation.

Its fine this time but next time copy and paste the text, otherwise if I need to copy and paste something to show you a change I can’t.

local GlowingPart = Instance.new("Part")
	GlowingPart.Size = Vector3.new(Item.Cube.Size.X + 0.05, Item.Cube.Size.Y + 0.05, 0.05)
	GlowingPart.Color = Color3.fromRGB(255, 255, 255)
	GlowingPart.Material = Enum.Material.Neon
	GlowingPart.CanCollide = false
	GlowingPart.Parent = game.Workspace
	
	RunServiceConnection = RunService.RenderStepped:Connect(function()
		GlowingPart.CFrame = CFrame.new(Item.Cube.Position.X, Item.Cube.Position.Y, GetMouse.Hit.Z) * CFrame.Angles(0, math.rad(-Item.Cube.Rotation.Y), 0)
	end)

You’re trying to get it to slide like this right?

Yeah. I want to use the axis of the part depending on its rotation like in this picture.

Subtract the cube position from the mouse position.
Take that and Dot it with Cube.CFrame * YourAxis (for example Vector3.zAxis), this is the distance along the desired axis with 0 being aligned with the cube center.
Set the glowing part’s CFrame to the Cube’s CFrame + distance * YourAxis

1 Like

is this right?

RunServiceConnection = RunService.RenderStepped:Connect(function()
	local SubtractCubePosFromMousePos = GetMouse.Hit.Position - Item.Cube.Position
	local Distance = SubtractCubePosFromMousePos:Dot(Item.Cube.CFrame * Vector3.zAxis)
		
	GlowingPart.CFrame = Item.Cube.CFrame + Distance * Vector3.zAxis
end)

(I might only answer in a few hours again because its really late over here)

Yes but I realized I made a mistake, use this:

RunServiceConnection = RunService.RenderStepped:Connect(function()
	local SubtractCubePosFromMousePos = GetMouse.Hit.Position - Item.Cube.Position
	local WorldAxis = Item.Cube.CFrame:VectorToWorldSpace(Vector3.zAxis)
	local Distance = SubtractCubePosFromMousePos:Dot(WorldAxis)
		
	GlowingPart.CFrame = Item.Cube.CFrame + WorldAxis * Distance
end)
1 Like

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