Slide using raycasts and CrossProducts

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to create a slide like deepwoken’s.
  2. What is the issue? Include screenshots / videos if possible!
    The Issue is that i don’t undestand nor have a good grasp on how to use CrossProducts or Horizontal Tilts in my script.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve tried reading other forum posts to see if anyone share my same issues.
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

This is my current code:

local character = script.Parent
local hrp = character:WaitForChild("HumanoidRootPart")
local Humanoid = character:WaitForChild("Humanoid")
local RS = game:GetService("RunService")
local UIS = game:GetService("UserInputService")

local animation = script:WaitForChild("Slide")
local AniTrack = Humanoid:LoadAnimation(animation)
local thingstoignore = {}
local tweenService = game:GetService("TweenService")

local vis = require(script.CastVisuals)
local RaycastVisualiser = vis.new(Color3.new(0, 0, 1), workspace)

local function StartSlide()
	if thingstoignore then
		for i, v in pairs(workspace.Characters:GetChildren()) do
			table.insert(thingstoignore, v)
		end

		for i, v in pairs(workspace:GetChildren()) do
			if v:IsA("Part") and v.Name == "hitbox" then
				table.insert(thingstoignore, v)
			end
		end
	end

	local raycastParams = RaycastParams.new()
	raycastParams.FilterType = Enum.RaycastFilterType.Exclude
	raycastParams.FilterDescendantsInstances = {thingstoignore}
	
	if not hrp then
		warn("HumanoidRootPart not found!")
		return
	end

	local raycastresult = workspace:Raycast(hrp.Position, hrp.CFrame.UpVector * -8, raycastParams)

	if raycastresult then
		
		local hitPosition = raycastresult.Position
		local yAxis = Vector3.new(0,1,0)
		local hitNormal = raycastresult.Normal
		local DesiredMultiplier = 10
		local speed = hrp.Velocity.Magnitude

		local horizontalTilt = math.abs(raycastresult.Normal.X) + math.abs(raycastresult.Normal.Z) -- To know the strength of the tilt

		local crossProduct = yAxis:Cross(hitNormal) -- To know if player is sliding in same direction as slope
		
		print(horizontalTilt)
		print(crossProduct)
		
		local linearVelocity = Instance.new("LinearVelocity")
		linearVelocity.Attachment0 = hrp.RootAttachment
		linearVelocity.MaxForce = 25000
		linearVelocity.RelativeTo = Enum.ActuatorRelativeTo.Attachment0
		linearVelocity.VectorVelocity = Vector3.new(0, 0, -DesiredMultiplier)
		linearVelocity.Parent = hrp
	end
end

UIS.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then return end

	if input.KeyCode == Enum.KeyCode.C then
		AniTrack:play()
		StartSlide()
	end
end)

UIS.InputEnded:Connect(function(input, gameProcessed)
	if gameProcessed then return end

	if input.KeyCode == Enum.KeyCode.C then
		AniTrack:stop()
		for _, descendant in pairs(character:GetDescendants()) do
			if descendant:IsA("LinearVelocity") then
				descendant:Destroy()
			end
		end
	end
end)
1 Like