Velocity Based Movement System

I made this custom velocity based movement system but i have the issue that whenever i run or jump into a wall the velocity doesnt stop so you get stuck.

I want it so the velocity stops when i jump into a wall. I know its done with raycasting but i dont know how to send the raycast in the direction the player is moving and im not really good at scripting.

This is the code for the movement and attempt to detect the wall

--Services
local CA = game:GetService("ContextActionService")
local Player = game:GetService("Players")
local UIS = game:GetService("UserInputService")
local RunS = game:GetService("RunService")
local TS = game:GetService("TweenService")

--Variables
local plr = Player.LocalPlayer
local char = plr.Character
local hum: Humanoid = char:WaitForChild("Humanoid")
local hrp: Part = char:WaitForChild("HumanoidRootPart")

--MovementSettings
local speed = 16
local acc = 100

local att: Attachment = hrp:WaitForChild("RootAttachment")

local v = Instance.new("LinearVelocity")
v.Parent = hrp

v.Attachment0 = att
v.RelativeTo = Enum.ActuatorRelativeTo.World
v.VelocityConstraintMode = Enum.VelocityConstraintMode.Plane
v.PrimaryTangentAxis = Vector3.new(1,0,0)
v.SecondaryTangentAxis = Vector3.new(0,0,1)
v.MaxForce = 100_000

local GiveVelo = true

--States
local State

----Movement
local function Moving(dt)
	
	local params = RaycastParams.new()
	params.FilterType = Enum.RaycastFilterType.Exclude
	params.FilterDescendantsInstances = {char}
	
	
	--Bigger deacceleration when in the air
	local deacc = if hum.FloorMaterial == Enum.Material.Air then 1 else 16


	--Velocity based on movedirection
	local movedir = hum.MoveDirection
	local targetv = Vector2.new(movedir.X, movedir.Z) * speed
	local deltav = targetv - v.PlaneVelocity
	local accrate = if movedir.Magnitude > 0 then acc else deacc
	
	
	--Give Velocity
	if GiveVelo then
		v.PlaneVelocity += if hum.FloorMaterial == Enum.Material.Air then deltav * (accrate * dt)/20 else deltav * (accrate * dt)
	else
		v.PlaneVelocity = Vector2.new(0,0)
	end
	
	
	--States
	if hum.FloorMaterial ~= Enum.Material.Air then
		if movedir.Magnitude == 0 then
			State = "Standing"
		else
			State = "Moving"
		end
	else
		State = "Falling"
	end
	
	
	--Raycasting
	local Result = workspace:Raycast(hrp.CFrame.Position, Vector3.new(movedir.X,1, movedir.Z) * 1 , params)
	
	--Doesn't work
	if Result then
		GiveVelo = false
	else
		GiveVelo = true
	end

	
end

RunS.RenderStepped:Connect(function(dt)
	Moving(dt)
end)

I would appreciate it if someone could help me or tell me how i can improve this.

2 Likes
local player = game.Players.LocalPlayer
local character = player.Character
if not character then return end

local root = character:FindFirstChild("HumanoidRootPart")
if not root then return end

local velocity = root.AssemblyLinearVelocity
if velocity.Magnitude < 0.1 then return end -- Avoid casting when stationary

local direction = velocity.Unit -- Normalize to get the movement direction
local origin = root.Position
local distance = 100 -- Adjust as needed

local rayParams = RaycastParams.new()
rayParams.FilterDescendantsInstances = {character} -- Ignore the player
rayParams.FilterType = Enum.RaycastFilterType.Blacklist

local result = workspace:Raycast(origin, direction * distance, rayParams)

if result then
    print("Hit:", result.Instance, "at", result.Position)
end
1 Like

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