You can write your topic however you want, but you need to answer these questions:
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.
What is the issue? Include screenshots / videos if possible!
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.
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.
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)
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 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.
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.