How would i make the wallkick impulse smoother?

So i have a script for a wallkick mechanic of sorts, that checks if a wall is in a box, then raycasting a 5 stud short beam, which if hitting something will apply a impulse at that hit position. While this does work it is very janky, with the character being oriented sideways for a split second before applying the impulse, while also the impulse being somewhat random, always going in a singular direction if sideways. I would love to know if there is a better way to impulse the character or shoot the raycasts to make it more consistent.

local wkm = 1.5
local wk = 1

local rs = game:GetService("RunService")

local Player = game.Players.LocalPlayer
local Character = Player.Character
if Character == nil then Player.CharacterAdded:Wait() end
local Humanoid = Character:WaitForChild("Humanoid")

local Material = Enum.Material
local StateType = Enum.HumanoidStateType

local wkc = 0
local lastJump = false

rs.RenderStepped:Connect(function()
	if Humanoid.FloorMaterial == Material.Air or wkc == 0 then return end

	wkc = 0
	Humanoid.JumpHeight /= wkm
end)

local function Boost()
	if wkc >= wk then return end
	local opparams = OverlapParams.new()
	opparams.FilterDescendantsInstances = {Character:GetDescendants()}
	local partsinbox = workspace:GetPartsInPart(Character.WallkickBox, opparams)
	if #partsinbox ~= 0 then 

		if wkc == 0 then Humanoid.JumpHeight *= wkm end
		wkc += 1

		local DIST = 5

		local params = RaycastParams.new()

		for i=1,360 do
			local x = math.cos(math.rad(i))
			local z = math.sin(math.rad(i))
			local dirVec = Vector3.new(x,0,z) * DIST

			params.FilterDescendantsInstances = workspace.Debris:GetChildren()

			local raycast = workspace:Raycast(Character.HumanoidRootPart.Position, dirVec, params)

			if raycast == nil then continue end

			local hitvis = Instance.new("Part")

			hitvis.Shape = Enum.PartType.Ball
			hitvis.Size = Vector3.new(1,1,1)
			hitvis.Position = raycast.Position
			hitvis.Anchored = true
			hitvis.CanCollide = false
			hitvis.Transparency = 0.5
			hitvis.Parent = workspace.Debris

			Humanoid:ChangeState(StateType.Jumping)

			Character.HumanoidRootPart:ApplyImpulseAtPosition(Vector3.new(600,0,600), hitvis.Position)
			break
		end
	end
end

rs.RenderStepped:Connect(function()
	if Humanoid.Jump == true and Humanoid.FloorMaterial == Material.Air and lastJump == false then Boost() end
	lastJump = Humanoid.Jump
end)
2 Likes