I’m making a custom character movement using BodyForce and I am having some trouble with it.
so this is what I got so far: when the player presses “w” (forward) it does this
while wait() do
--print(WalkForward)
if WalkForward == true then
-- trying to get direction relative to camera, setting the BodyForce to (0,0,-1000) will move it forward but not by the camera
local Direction = game.Players.LocalPlayer.Character.HumanoidRootPart.Position*workspace.CurrentCamera.CFrame.Position
--game.Players.LocalPlayer:Move(Vector3.new(0, 0, -1), true)
if not game.Players.LocalPlayer.Character.HumanoidRootPart:FindFirstChild("Forward") then
local bodyForce = Instance.new("BodyForce")
bodyForce.Parent = game.Players.LocalPlayer.Character.HumanoidRootPart
bodyForce.Name = "Forward"
game.Players.LocalPlayer.Character.HumanoidRootPart:FindFirstChild("Forward").Force = Direction -- sets the direction
else
game.Players.LocalPlayer.Character.HumanoidRootPart:FindFirstChild("Forward").Force = Direction
end
end
end
I am trying to make the player move in the direction of where the camera is facing.
How can I make it so that it can make the BodyForce move in the direction of the camera?
Then, since this is a unit vector, we need to multiply it by some walk speed value like 20 or something to make us go a running speed.
Now we can set the velocity of the BodyVelocity to this. You can tweak the values of the BodyVelocity so you get enough force to move the character correctly without it being so powerful that it will fling when you hit a wall.
local Camera = workspace.CurrentCamera.CFrame
while wait() do
--print(WalkForward)
if WalkForward == true then
local Direction = Vector3.new(Camera.LookVector.X,0,Camera.LookVector.Z)*1000
--game.Players.LocalPlayer:Move(Vector3.new(0, 0, -1), true)
if not game.Players.LocalPlayer.Character.HumanoidRootPart:FindFirstChild("Forward") then
local bodyForce = Instance.new("BodyForce")
bodyForce.Parent = game.Players.LocalPlayer.Character.HumanoidRootPart
bodyForce.Name = "Forward"
game.Players.LocalPlayer.Character.HumanoidRootPart:FindFirstChild("Forward").Force = Direction
else
game.Players.LocalPlayer.Character.HumanoidRootPart:FindFirstChild("Forward").Force = Direction
end
--WalkForward = false
end
end
sorry, it doesnt work for me, am I setting the direction correctly?, this just moves the player forward regularly