Coding a physics based movement system

I don’t really ask many questions here so it would be sad if this gets deleted like all my other scripting support questions because they were not answered.

Anyways, I am making a physics based movement system using apply impulse but the issue is that apply impulse stacks and I do not want that because it makes the player go unrealistically fast.

A topic that I viewed here:

Is close to the solution but the user is using body velocity which are not too physics based because I need gravity. I won’t code gravity because, yet again, it is unrealistic.

My question is:

  • How can I limit apply impulse?

My code:

local Character = script.Parent
local Humanoid:Humanoid = Character:WaitForChild("Humanoid")
local Camera = workspace.CurrentCamera
local HRP:BasePart = Character:WaitForChild("HumanoidRootPart")

local UIS = game:GetService("UserInputService")
local RS = game:GetService("RunService")

local oldvelocity = Vector3.zero
local speed = 16
local currentspeed = speed
local jumpheight = 7

local function KD(Key)
	return UIS:IsKeyDown(Enum.KeyCode[Key])
end

local function GetMass()
	local mass = 0
	for _,v in pairs(Character:GetChildren()) do
		if v:IsA("BasePart") then
			mass += v:GetMass()/v.CurrentPhysicalProperties.Density
		end
	end
	return mass
end

local maxy = 0

RS.RenderStepped:Connect(function(dt)
	local x = KD("A") and -1 or KD("D") and 1 or 0
	local z = KD("W") and -1 or KD("S") and 1 or 0
	
	local _,y_rot,_ = Camera.CFrame:ToOrientation()
	local dir = CFrame.Angles(0,y_rot,0)*CFrame.new(Vector3.new(x,0,z).Unit)
	
	local velocity = dir.Position*(currentspeed)*GetMass()
	currentspeed = speed+math.abs((HRP.AssemblyLinearVelocity.Y/(4+math.pi)))
	
	HRP:ApplyImpulse(velocity)
	
	oldvelocity = velocity
end)

I also tried using deltatime but that just slows stuff by a lot.

I tried to explain this as briefly and accurately as I can.

Detail

I am trying to code a physics based movement system using applyimpulse on the humanoid root part but applyimpulse will not stop stacking. I really do not want that because it makes my character go extremely fast. I was previously using humanoid:moveto which did kind of worked but it just felt yet again unrealistic. So is there a way to make it physics based with velocity? Or realistic with humanoid:moveto?

I also do not want to use linearvelocity as gravity and collisions are not really simulated while using linearvelocity which I obviously, yet again, do not want.

Check out the other MoverConstraints like VectorForce or LinearVelocity to apply a consistant force instead of a sudden impulse.

This is something else but could i suggest that you use something like this for detecting movement:

local xWalkingDirection = humanoid.MoveDirection:Dot(rootPart.CFrame.RightVector) :: number
local zWalkingDirection = humanoid.MoveDirection:Dot(rootPart.CFrame.LookVector) :: number
– -1 is left or forward, 1 is right or backward

this way you have mobile and console players without the need of WASD

I don’t think this works if movement is set to scriptable. Or does it? Yea it doesn’t so that is stupid.

I have said that linearvelocity or bodyvelocity does not work and so doesn’t vectorforce as it accelerates it.

UPDATE
You are right but I won’t be marking yours as solution due to well not sufficient information provided. Technically this topic:

Worked because I did not knew that existed.

i have a custom movement system and it works just fine

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.