The Parts should turn towards the click at the same degree

Hello!
My idea is to create a system like this: change the direction of the parts depending on where they are clicked, as shown in the picture:

what I want to achieve:
Снимок экрана 2024-10-18 202024
Note: sticks indicate the direction of the parts after clicking


The problem is that I don’t know how to do it exactly as I showed.
All I could do was rotate them to where the click was made.
my poor attempt at creating this system looks like this.
Снимок экрана 2024-10-18 202024 в2

main part of this system that I got:

for i, part in pairs(parts) do
  local Direction = Vector3.new(MousePos.X, 0, MousePos.Z) 
  part.CFrame = CFrame.lookAt(part.Position, Direction)
end

Note: There are some more functions there, that’s why I use lookAt()

1 Like

To do what you want (the first image), you only need to change the CFrame of one brick! Because you orient each brick individually, you are basically asking each brick to make its own calculations for how it should turn.

Choose one of the bricks that you want to be the ‘primary’ part, and calculate based on that brick only. Once you calculate it for this brick, you can ‘copy’ that CFrame into the other bricks, and they will all face the same direction.

local PrimaryBrick = [INSERT YOUR PRIMARY BRICK HERE!]
local Direction = Vector3.new(MousePos.X, 0, MousePos.Z) 
PrimaryBrick.CFrame = CFrame.lookAt(PrimaryBrick.Position, Direction)

for i, part in pairs(parts) do
  if not part = PrimaryBrick then
    part.CFrame = CFrame.lookAt(part.Position, PrimaryBrick.CFrame.LookVector)
  end
end

A bit rusty with CFrames but this should at least get you close. You might have to play around with the ‘direction’ part of the CFrame.lookAt() method in the loop.

3 Likes

I used XZ to make the effect on a 2d plane and then rotated the part using CFrame. If I understood your issue, this should fix the problem. The code is self explanatory

local function updatePartOrientation(part, clickPosition)
	local PartPos = part.Position

	local Direction = Vector3.new(clickPosition.X - PartPos.X, 0, clickPosition.Z - PartPos.Z).Unit

	local Angle = math.atan2(Direction.X, Direction.Z)

	local NewCFrame = CFrame.new(PartPos) * CFrame.fromEulerAnglesYXZ(0, Angle, 0)

	part.CFrame = NewCFrame
end

Full Code I used for testing:

local Players = game:GetService("Players")
local Workspace = game:GetService("Workspace")

local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()

local function updatePartOrientation(part, clickPosition)
	local PartPos = part.Position

	local Direction = Vector3.new(clickPosition.X - PartPos.X, 0, clickPosition.Z - PartPos.Z).Unit

	local Angle = math.atan2(Direction.X, Direction.Z)

	local NewCFrame = CFrame.new(PartPos) * CFrame.fromEulerAnglesYXZ(0, Angle, 0)

	part.CFrame = NewCFrame
end

local function onMouseClick()
	local clickPosition = Mouse.Hit.Position

	for _, part in ipairs(Workspace:GetChildren()) do
		if part.Name == "Part" then
			updatePartOrientation(part, clickPosition)
		end
	end
end

Mouse.Button1Down:Connect(onMouseClick)

I didnt understand which image u needed help with sorryz

2 Likes

thanks for the answer
Well I should have explained a little better what goal I want to achieve
the result is like in this screenshot this is what I want:
Снимок экрана 2024-10-18 202024


As I understand it, your code does what I showed in screenshot 2, however, it was something that I already had(

1 Like

From what I see in your pictures, the closest brick is the determining factor.
Looks like you need to get the closest Part to the click, face it toward the click, and copy that CFrame to all the other Parts.

2 Likes

Yes, this solution has already been offered by notsfeelings. Now I’m trying to slightly adjust the existing script and implement the system he proposed, after which I hope the problem will be solved

1 Like

Yes, but the description didn’t mention which brick to choose.
Check all the Magnitudes from the click to each Part you want rotated, choose the lowest Magnitude, then perform @notsfeelings code.

3 Likes

Then you should mark it as the solution.

1 Like

this is true but I would call it just a temporary solution I haven’t tried it yet however it is clearly either a solution or very close to a solution

1 Like

This is why you haven’t solved your problem yet. @notsfeelings’s post says the same thing; read it and implement his code.

2 Likes

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