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.
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
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.
That's one line :P
Torso.CFrame = CFrame.new(Torso.Position,Vector3.new(Mouse.Hit.p.X, Torso.Position.Y, Mouse.Hit.p.Z))