Make 2 parts face against each other

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to make 2 parts face against each other, depending on mouse position.
  2. What is the issue? Include screenshots / videos if possible!
    RobloxStudioBeta_xXIOdAFp1k
    I just don’t understand how to make this, and I can not find anything on the dev hub about it. Above is a screenshot describing what I want to achieve.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have looked it up in google, dev hub and dev forums, but I can’t find any solution on it, only stuff about making parts face something, etc etc. I know how to do the mouse position thing, but I do not know how to make the parts move and rotate like that to the mouse.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

Hello!
Making 2 parts face each other can be done with some quick CFrame manipulation.

To understand how it would be done, you should first understand that CFrame.new() can take a plethora of arguments (find a full explanation of CFrame manipulation here).

Two of the important arguments we need now are the first 2, which can both be Vector3s. The first one would be the position to set the part at, and the second would be the position the part looks at.

Using this knowledge, we can now answer: How to make 2 parts face each other?

local part1 = workspace.Part1;
local part2 = workspace.Part2;

part1.CFrame = CFrame.new(part1.Position, part2.Position);
part2.CFrame = CFrame.new(part2.Position, part1.Position);

I do not quite understand what you mean by “depending on mouse position”.

I hope I’ve clarified enough! If you need anything else, please do not hesitate to let me know.

3 Likes

By mouse position what I mean is, making the parts face the mouse, but the left part faces the opposite direction instead.

Sort of like this:image

So like, part 1 faces the mouse, but part 2 faces in the exact opposite direction of the mouse?

Yeah, that’s what I want. Basically everytime I move the mouse , the two parts move.

CFrame.new(Vector3, Vector3) is deprecated, you should be using CFrame.lookAt(Vector3, Vector3)

local part1Pos = workspace.Part1.Position
local part2Pos = workspace.Part2.Position

part1.CFrame = CFrame.lookAt(part1Pos, part2Pos)
part2.CFrame = CFrame.lookAt(part2Pos, part1Pos)
1 Like

Alright, lets do this!

The mouse is a client-sided instance only, and passing it continuously to the server to update the CFrame of a part super-quick would present some issues. So, lets render it on the client.

Note: This code has not been tested, but should work.

Note 2: Thank you, Omicron, for letting me know that CFrame.new(Vector3, Vector3) is deprecated. I’ve updated the code snipped in this response accordingly!

local player = game.Players.LocalPlayer; -- Get the local player
local m = player:GetMouse(); -- Get the player's mouse

local position = m.Hit.p; -- The position of where the mouse hit (mouse.Hit would return a CFrame by default)

local part1, part2 = workspace.Part1, workspace.Part2; -- Part 1 and part 2

function update() -- function to update the parts' position and what they are looking at
     part1.CFrame = CFrame.lookAt(part1.Position, position); -- Make part1 stay in place and face towards the mouse
    part2.CFrame = CFrame.lookAt(part2.Position, position) * CFrame.Angles(0, math.pi, 0); -- Make part2 face the mouse, then rotate it by pi radians along the Y axis
end

update(); -- Update the parts initially

m.Move:Connect(function() -- RBXConnection to the 'Move' event of the Mouse. Better than looping because this fires everytime the mouse moves
    position = m.Hit.p; -- Update position
    update();
end)

Thanks! Just one little problem, the parts are facing the wrong direction, what can I do to solve this?

Could you define what you mean by wrong direction? part1 should be facing the mouse, and part2 should be facing the side opposite of the mouse. Do you mean by their positions?

I think I get what you mean. Let me try to get some code written, and you could let me know if it’s what you intended.

I am trying to make a Roblox game where you can use VR with your mouse, so the parts should be facing the mouse like an arm, but instead they are facing like a door.

Also, looping the part infront of the player’s root part flickers it in a very weird way, how would I fix this?

Did you mean something like this?
https://gyazo.com/6e8f83895ad9ca066552b16cd7b09a1c

Yes, that is what I meant, but I actually fixed it by changing the size of the parts.

Yeah lol, the last code should work if you adjust the size. Hope I was able to help!

Thanks! But how would I go about putting the parts infront of the player? I don’t know how to multiply Vector3 properly

If you want to keep the parts stuck in-front of the player and face towards the mouse, you should use Motor6D. It would be similar to the scripts that cause your player to turn their head depending on where the mouse is. I’ll write some code and reply here once it’s ready.

1 Like