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

Making a BodyPosition point towards the cursor?

Asked by 7 years ago
Edited 7 years ago

So i've been trying something lately,trying to script a sword attack,the projectile works well,but i can't make the body point towards the mouse.

Im using BodyPosition,how can you make it point towards the mouse using a loop or anything else?

I understand this might look like a request,which is against the rules. But still,i need help..

1 answer

Log in to vote
0
Answered by
Master_JJ 229 Moderation Voter
7 years ago

There was actually an article about this in the blog that may help. You can find it here

One way to do it is to use direction. Here is the code:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()


local character = player.Character or player.CharacterAdded:wait()
local humanoid = character:WaitForChild("Humanoid")
local torso = character:WaitForChild("Torso")


local runService = game:GetService("RunService")


humanoid.AutoRotate = false


runService:BindToRenderStep("MouseMovement", Enum.RenderPriority.Input.Value, function()
    local direction = mouse.Hit.p - torso.Position
    direction = direction * Vector3.new(1, 0, 1)
    torso.CFrame = CFrame.new(torso.Position, torso.Position + direction)
end)

So first I define my variables: player, mouse, character, etc.

Then I disable AutoRotate, a property of the humanoid.

Then I bind a function to RenderStep. You will get the same results if you use a while true do loop, but this makes it a bit more smoother.

In the function, we get the direction by subtracting the mouse's position and the torso's position. Then we multiply it by `Vector3.new(1, 0, 1) to get rid of the why value, this is to prevent the player from looking upwards. Finally we set the torso's CFrame to face the direction of the mouse.

0
Thanks! ,though i wanted it to point upwards too,but i got the answer i wanted. kristibezatlliu1111 33 — 7y
0
If you want to make it point upwards too, remove line 18. Master_JJ 229 — 7y
0
Also,how can i end the script so it wont point to the mouse anymore? kristibezatlliu1111 33 — 7y
Ad

Answer this question