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

How can I make the camera face the mouse?

Asked by 8 years ago
Edited 8 years ago

I want to write code that will always make the player's camera look at the position where the player's mouse is at any given time. However, I am a bit stuck as to how to do this.

So on the ROBLOX Wiki it says that you can no longer change the Camera's Focus, so now I'm at a lost as to how to tell the Camera where to look.

0
I understand where you're coming from, do you mean like design it? Zyrup 27 — 8y

2 answers

Log in to vote
2
Answered by 8 years ago

To start off, I'll introduce a body force that you may or may not have heard of, BodyGyro. Put simply, a BodyGyro will influence the rotation of whatever BasePart it is parented to. With this said, we'll be using this as our main means for making our character face his or her camera.

I should also mention that we can set the CFrame of our character without he or she dying, this is because it doesn't break joints or reset welds or anything of that sort. So that's where we'll be putting our BodyGyro.

local player, bG = game.Players.LocalPlayer, Instance.new("BodyGyro", game.Players.LocalPlayer.Character.Torso)

This will make the BodyGyro and put it in our characters torso, but we haven't defined the MaxTorque of the gyro. We need to set it to something that won't cause our character to do back and front flips out of control. For this, we'll set it to 10,000 on the Y-Axis.

bG.MaxTorque = Vector3.new(0, 10000, 0)

So, now we've set up the BodyGyro. Next we just need to set up a loop to set the CFrame of the gyro to face the mouse. For this, we'll get the mouse, and set the CFrame.

while wait() do
 bG.CFrame = CFrame.new(player.Character.Torso.Position, player:GetMouse().Hit.p)
end

Hope this helped.

Also, I apologize if I have a flawed understanding and have given poor or inaccurate explainations.

0
Sorry if I wasn't clear, but I want to player's camera to always look at where the player's mouse is pointing to. VongolaXII 15 — 8y
0
Don't use GetMouse LightModed 81 — 8y
0
I'm not sure if that's possible. You can also always just enable permanent FPS mode in StarterPlayer, I think MINEBLOX106 55 — 8y
Ad
Log in to vote
0
Answered by 8 years ago

Do you mean that the player will look in the direction of the cursor? If so, you have to change the player's torso's CFrame to face in the direction that you are pointing. The variables are so the none of the lines are too long. You can remove them if you want.

local lp=game:GetService("Players").LocalPlayer
local m =lp:GetMouse''
local t =lp.Character.Torso

while wait() do
    t.CFrame=CFrame.new(t.Position,Vector3.new(m.Hit.p.X,t.Position.Y,m.Hit.p.Z))
end

Answer this question