Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 7 years ago

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

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

1 answer

Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
7 years ago
Edited 7 years ago

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)
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? SilentAim 36 — 7y
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? SilentAim 36 — 7y
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 Goulstem 8144 — 7y
0
It doesn't work with multiple players because each client is attempting to rotate the same object. Hence line 3 Goulstem 8144 — 7y
0
^ Would this put a lot of stress on the server? Since it'll be constantly updating the rotation of the brick? SilentAim 36 — 7y
Ad

Answer this question