Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Making a BodyPosition point towards the cursor?

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

✓ Accepted answer

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.

3 comments

0

Thanks! ,though i wanted it to point upwards too,but i got the answer i wanted.

0

If you want to make it point upwards too, remove line 18.

0

Also,how can i end the script so it wont point to the mouse anymore?

This discussion is preserved as it was. New answers, comments, and votes are closed.