Hello, I’ve been trying to make realistic / gradually accelerating movement for my game simular to that of forsaken.
My issue:
It works but whenever I jump into a wall I stay clinging to the wall.
Example Video:
My script:
local RunService = game:GetService("RunService")
local Playerz = game:GetService("Players")
local Player = Playerz.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Root = Character:WaitForChild("HumanoidRootPart")
local RootAtt = Root:WaitForChild("RootAttachment")
local Acceleration = 8
local Deacceleration = 12
local Velocity = Instance.new("LinearVelocity")
Velocity.Attachment0 = RootAtt
Velocity.Name = "Mover"
Velocity.Parent = Root
Velocity.RelativeTo = Enum.ActuatorRelativeTo.World
Velocity.VelocityConstraintMode = Enum.VelocityConstraintMode.Plane
Velocity.PrimaryTangentAxis = Vector3.new(1, 0, 0)
Velocity.SecondaryTangentAxis = Vector3.new(0, 0, 1)
Velocity.MaxForce = 100000
RunService.PreRender:Connect(function(deltaTime)
local VelocityMovement = coroutine.wrap(function()
local MoveDirection = Humanoid.MoveDirection
local TargetVelocity = Vector2.new(MoveDirection.X, MoveDirection.Z) * Humanoid.WalkSpeed
local DeltaVelocity = TargetVelocity - Velocity.PlaneVelocity
local AccelerationRate = if MoveDirection.Magnitude > 0 then Acceleration else Deacceleration
--print(math.ceil(Velocity.PlaneVelocity.Magnitude))
Velocity.PlaneVelocity += DeltaVelocity * AccelerationRate * deltaTime
end)
VelocityMovement()
end)
What I tried to fix this:
Set all parts in the character to massless.
Change the velocity from plane velocity to Vector velocity.
Change player physicalproperties.
1 Like
Well, you can imagine that pressing hard against a surface would increase the friction you experience against it. That is what is happening here, your character is experiencing a force pushing them into the wall, and so you experience increased friction and slowed acceleration from gravity.
You can add a check to make sure the character is not pressing into a wall by raycasting/shapecasting in the direction of their velocity with respect to the deltaTime
. If it hits something, they are walking into something.
You could also make the player absolutely slibbery
2 Likes
I fixed it by changing the velocitys max force to be a little less and changing the physical properties of the character
-- StarterPlayer > StarterCharacterScripts
local RunService = game:GetService("RunService")
local Playerz = game:GetService("Players")
local Player = Playerz.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Root = Character:WaitForChild("HumanoidRootPart")
local RootAtt = Root:WaitForChild("RootAttachment")
local Acceleration = 6
local Deacceleration = 8
local Velocity = Instance.new("LinearVelocity")
Velocity.Attachment0 = RootAtt
Velocity.Name = "Mover"
Velocity.Parent = Root
Velocity.RelativeTo = Enum.ActuatorRelativeTo.World
Velocity.VelocityConstraintMode = Enum.VelocityConstraintMode.Plane
Velocity.PrimaryTangentAxis = Vector3.new(1, 0, 0)
Velocity.SecondaryTangentAxis = Vector3.new(0, 0, 1)
Velocity.MaxForce = 10000
local customProperties = PhysicalProperties.new(
0.8,
0.3,
0.5,
1,
1
)
local function OnCharacterAdded()
for i, v in pairs(Character:GetDescendants()) do
if v:IsA("BasePart") and v.Name ~= "Accessory" and v:IsA("Accessory") == false then
v.CustomPhysicalProperties = customProperties
end
end
end
RunService.PreRender:Connect(function(deltaTime)
local VelocityMovement = coroutine.wrap(function()
local MoveDirection = Humanoid.MoveDirection
local TargetVelocity = Vector2.new(MoveDirection.X, MoveDirection.Z) * Humanoid.WalkSpeed
local DeltaVelocity = TargetVelocity - Velocity.PlaneVelocity
local AccelerationRate = if MoveDirection.Magnitude > 0 then Acceleration else Deacceleration
--print(math.ceil(Velocity.PlaneVelocity.Magnitude))
Velocity.PlaneVelocity += DeltaVelocity * AccelerationRate * deltaTime
end)
VelocityMovement()
end)
Player.CharacterAppearanceLoaded:Connect(OnCharacterAdded)
Why I didn’t choose to use raycast? I’m making a combat game which will really limit the player’s movement if I stop the velocity when it hits a target, for example when I want to make a crater on the ragdoll and it get’s stopped because of the velocity raycast this will limit it, obviously you can fix this but I thought this way was more efficient and simple. Thanks to @azavier123 I was able to figure this out.
Thank you for posting this publicly! this is pretty performant and quite nice…
I may include it in my resource FPSys.
I will credit you with a link and even by name if you so request.