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..
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.