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

How do you make a character follow their mouse?

Asked by 8 years ago

I know it has something to do with math.asin and targetfilter, but how do I do it.

You don't have to do the script, but point me in the right direction.

0
Honestly you could just forced players to use ROBLOX's `click to move` instead of WASD. This would remove WASD use though alphawolvess 1784 — 8y

3 answers

Log in to vote
6
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
8 years ago

It's much easier than you're thinking.

You can use mouse.Hit.p to get the correct coordinates.

The constructor CFrame.new(Vector3 position, Vector3 lookAt) is pretty useful. Using this constructor, one might think that the following would be sufficient:

while wait() do
    torso.CFrame = CFrame.new(torso.Position, mouse.Hit.p)
end

But in actuality this won't work well because it doesn't account for the mouse's Y coordinate. This means that if you move your mouse upwards, your character will float. We don't want that.


So we gotta do a bit more math. The first step is to get the direction we want the torso to face. This is done with subtraction, the formula being to - from.

local direction = (mouse.Hit.p - torso.Position)

But how does subtraction get the direction? This confused me for a pretty long time. To understand it, we need to eliminate all variables.

Let's just take two numbers -- 5 and 3. Let's assume they're on the Y axis, so 5 will be higher than 3.

5 - 3 = 2

If we're at 3, how do we get to five? We go up two studs. But wait! That was the same result we got with subtraction! So as you can see, subtraction gives the direction we need to travel to reach the destination.


But this direction still has the Y axis, so our character will still float. How do we get rid of this Y value? Multiply it by 0.

local direction = (mouse.Hit.p - torso.Position) * Vector3.new(1, 0, 1)

Now all we do is add our direction to the torso's current position. Remember, if we're at 3 we need to go (or add) 2 if we wish to reach 5. Then just put this is our handy CFrame constructor.

torso.CFrame = CFrame.new(torso.Position, torso.Position + direction)

Final code (minus variable definitions):

while wait() do
    local direction = (mouse.Hit.p - torso.Position) * Vector3.new(1, 0, 1)
    torso.CFrame = CFrame.new(torso.Position, torso.Position + direction)
end
0
+1, very good, thorough answer. DreadPirateRobux 655 — 8y
0
As Dread said, you certainly deserve it! Thank you. SimplyRekt 413 — 8y
Ad
Log in to vote
0
Answered by 8 years ago

Well, I believe mouse.Hit gives you the coordinates of where your mouse is hovering over. If you hover over the skybox, it returns nil, so I advise making a function like:

if mouse.Hit == nil then
return end

Anyway. There's a function for the player that is:

player.Move (Vector3 walkDirection, bool relativeToCamera = false)

So basically, make variables that represent that mouse.Hit.X and mouse.Hit.Z (for y, I think you'd just want to keep that to whatever the player's current Y value is, although I may be wrong)

Let me know if that works, because I didn't test it.

Log in to vote
0
Answered by
22ron 0
8 years ago

That's one line :P

Torso.CFrame = CFrame.new(Torso.Position,Vector3.new(Mouse.Hit.p.X, Torso.Position.Y, Mouse.Hit.p.Z))

Answer this question