Sliding with Raycasting?

I’m creating an Inventory system, and I’m trying to give my items their own custom physics.

I’ve set up the items to be above any surface by 6 studs(can change in the future), and I’m trying to figure out a way I can use normals to create a sliding effect, whenever the objects are on slopes.

Here’s my current testing code:

game:GetService("RunService").Heartbeat:Connect(function()
	


	local RayPart = script.Parent

	
	local RayCastParams = RaycastParams.new()
	RayCastParams.FilterType = Enum.RaycastFilterType.Blacklist
	RayCastParams.FilterDescendantsInstances = {Norm, RayPart}


	
	local RayCast = workspace:Raycast(RayPart.Position, RayPart.CFrame.UpVector * -1000, RayCastParams)
	if RayCast then
	

RayPart.CFrame = CFrame.new(RayCast.Position + Vector3.new(0,6,0))
	--print("e")
	end


end)

In this image, I want the Item above, to slide down to the position of the blue item.

For use normals, you can use a 'RaycastResult; in your code the ‘RaycastResult’ is the ‘RayCast’ variable. You need get the normal using:

local normal = RayCast.Normal

and then edit the orientation of the Object as works best.

For more information of this you can read the ‘RayCastResult’ API here:

1 Like

I know how to get the normal, I just don’t know how I can use the normal to move the object down a slope.

I can give you a table, comparing the normal vector with their approximate interpretation:

vector3.new(0, 1, 0) = the top face of a part with the Y world axis.
vector3.new(0, -1, 0) = the bottom face of a part with the Y world axis.
vector3.new(1, 0, 0) = the right face of a part with the X world axis.
vector3.new(-1, 0, 0) = the left face of a part with the X world axis.
vector3.new(0, 0, 1) = the ** back** face of a part with the Z world axis.
vector3.new(0, 0, -1) = the front face of a part with the Z world axis.
vector3.new(0, 0.707, 0.707) = the top face of a part with orientation in (45,0,0).
vector3.new(0, -0.707, -0.707) = the bottom face of a part with orientation in (45,0,0).
vector3.new(0, 0.707, 0.707) = the front face of a part with orientation in (45,0,0).
vector3.new(0, -0.707, -0.707) = the back face of a part with orientation in (45,0,0).
vector3.new(0.707, 0.707, 0) = the right face of a part with orientation in (0,0,45).
vector3.new(-0.707, -0.707, 0) = the left face of a part with orientation in (0,0,45).

(the faces of the block were taken with base in athe’face’ property of a ‘decal’ object)

The normal vector is the tangent of the angle obtained between the ray and the respective world axes, for this reason, in the test with a block with orientation in 45 in some axis, the normal vector is the tangent of 45° (0.7071) according the axis. Probably you need to know trigonometry for understand the normals perfectly.

1 Like

I made a new post on this

Help appreciated

1 Like

You can get the horizontal XZ component of the normal vector which is the direction to slide down.

RayPart.CFrame = CFrame.new(RayCast.Position + Vector3.new(0,6,0))
local normalHorizontalComponent = RayCast.Normal*Vector3.new(1,0,1)
if normalHorizontalComponent .Magnitude > 0.001 then
RayPart.CFrame += normalHorizontalComponent.Unit * 0.1
end
3 Likes

How do i add it to a script for sliding