I am making my own character movement system, and to move a character, in this case just a simple cylinder, I use VectorForce constraint. But the problem I am encountering is that the force that I am calculating, is just too low for the character to move.
If I am not mistaken, then the formula for calculating force is: (v-u)/t * m
and here’s the code implementation of this:
function Character:PreSimUpdate(deltaTime)
local model = self.__Model
local character: BasePart = model.Character
local camController: CameraController.CameraController = Client.GetInstance().__CameraController
local camCframe = camController:GetCameraCFrame()
local x, y, z = camCframe:ToEulerAnglesYXZ()
local calculator = CFrame.fromEulerAngles(0,y+math.rad(90),0)
if(UserInputService:IsKeyDown(Enum.KeyCode.D)) then
calculator *= CFrame.new(0,0,1)
end
if(UserInputService:IsKeyDown(Enum.KeyCode.S)) then
calculator *= CFrame.new(-1,0,0)
end
if(UserInputService:IsKeyDown(Enum.KeyCode.A)) then
calculator *= CFrame.new(0,0,-1)
end
if(UserInputService:IsKeyDown(Enum.KeyCode.W)) then
calculator *= CFrame.new(1,0,0)
end
local targetVelocity = Vector3.new(calculator.Position.X, 0, calculator.Position.Z)
if(targetVelocity.Magnitude ~= 0) then
targetVelocity = targetVelocity.Unit * 16
end
character.VectorForce.Force = (targetVelocity - character.AssemblyLinearVelocity)/1 * character.AssemblyMass
end
This function is called by RunService.PreSimulation event.
As for some reason this force is too low to move the character. I tried multiplying the target velocity by 5 and the thing moves.
I am not sure if this is an error on my end or something is up with the VectorForce constraint.
Adding to this, I also have an AlignOrientation constraint in the cylinder that keep it upright, and its RigidityEnabled property is set to true.
Video demos:
Without multiplying the targetVelocity by 5:
With multiplying the targetVelocity by 5: