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

How do I find the direction the mouse is pointing in? What about it's location?

Hi, I'm trying to make a brick point where the mouse is pointing using body gyro! Any ideas? Thanks!

1 comment

0

Use the CFrame.new(startPosition, lookAt) constructor. I'm not gonna bother with a full answer because you didn't supply any code

1 answer

✓ Accepted answer

To do this, you're going to have to have some insight on CFrames.

There is a CFrame constructor that takes in 2 arguments:

  • The Vector3 origin
  • The Vector3 look point

Pretty self explanatory.


If you use the CFrame constructor, you can provide the brick's Position as the origin argument, and the mouse's hit Position as the look point. This will create a CFrame that is located at your object, and looking at your mouse.

Then all you have to do is apply the CFrame to a BodyGyro object.

local plr = game.Players.LocalPlayer;  --The player
local mouse = plr:GetMouse();	       --The mouse
local obj = workspace.Part1;           --The object

game:GetService("RunService").RenderStepped:connect(function()	
	--CFrame construct	
	local cf = CFrame.new(obj.Position,mouse.Hit.p);
	--Locate the BodyGyro
	local bg = obj:FindFirstChild("BodyGyro");
	--Manipulate it
	bg.P = obj:GetMass()*1000;
	bg.D = 500;
	bg.MaxTorque = Vector3.new(5000,5000,5000);
	bg.CFrame = cf;
end)

5 comments

0

It works awesome, thanks for the explanation! However, I'd like everyone in the server to be able to see the brick rotating, but the script is only working in a local script, is there any way to change it to a server script so everyone can see the brick rotate?

0

^ To clarify, my intention is to create a new brick per player that will use the local script to make the brick point that way, but if I'm making the brick point that way in a local script won't only the specific player be able to see it?

0

You're going to need to use Remotes to make this effect replicate to the server. Here's a guide: http://wiki.roblox.com/index.php?title=RemoteFunction_and_RemoteEvent_Tutorial

0

It doesn't work with multiple players because each client is attempting to rotate the same object. Hence line 3

0

^ Would this put a lot of stress on the server? Since it'll be constantly updating the rotation of the brick?

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