Angle player to slope?

im making a skating game and im trying to make the player rotate one the slant instead of lipping through the ramp (example in video). I have gotten the raycasting done but i just cant get the player to rotate in the right axis.


I want it to look like this, but i cant figure it out.

any help is appreciated, heres my code. Its in a local script under startercharacter.

local RunService = game:GetService("RunService")

local Character = script.Parent
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local LowerTorso = Character:WaitForChild("LowerTorso")
local Root = LowerTorso:WaitForChild("Root")
local C1 = Root.C1
local RayParams = RaycastParams.new()
RayParams.FilterDescendantsInstances = {Character}
RunService.RenderStepped:Connect(function()
	local RaycastResult = workspace:Raycast(HumanoidRootPart.Position,Vector3.new(0,-10.5, 0), RayParams)
	if RaycastResult then
		local pitch = math.asin(-RaycastResult.Normal:Cross(HumanoidRootPart.CFrame.rightVector).Y);
		Root.C1 = C1 * CFrame.Angles(pitch , 0, 0)
		Root.C1 = Root.C1:ToWorldSpace(CFrame.New(0,Root.C1.LookVector.Y,0))
	else
		Root.C1 = C1
	end
end)

RaycastResult also returns a value called Normal, which is basically the vector and angle which is facing towards the Ray. So you can use the Normal value to position the angle that you’re looking for.

RaycastResult.Normal
1 Like

how would i implement that into my code?

Here’s a good example: Rotate part in the direction of the raycast normal

You could do:

local x,y,z = CFrame.new(Vector3.zero,Raycast.Normal):ToOrientation()

To get the rotation of that normal and then just apply it to the humanoid with some offsets.

hmm, sounds tedious. could you give me an example?

This is a cool project though, I wish there were more skating games in Roblox, good luck on your project bro!

1 Like

No not really too tedious, it’s as simply as setting the root cframe to:

local rotation = CFrame.new(Raycast.Normal, Vector3.zero)*CFrame.Angles(math.rad(90),0,0)

I would highly suggest that you use align-orientation on one attachment mode as well as it’s much more easier are smooth.

im pretty new to this stuff, do you think you could give me a code example with the align-orientation?

thanks man, i noticed all the skate games suck so i might aswell try lmao

1 Like

Thus method below should do the trick however its in world space which needs to be converted to c1. You can do this by manipulating the weld/joint formula.

We aren’t really allowed to hand out full code snippets but as you are new:

local RunService = game:GetService("RunService")

local Character = script.Parent
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local LowerTorso = Character:WaitForChild("LowerTorso")
local Root = LowerTorso:WaitForChild("Root")
local C0 = Root.C0
local RayParams = RaycastParams.new()
RayParams.FilterDescendantsInstances = {Character}

RunService.RenderStepped:Connect(function()
	local RaycastResult = workspace:Raycast(HumanoidRootPart.Position,Vector3.new(0,-10.5, 0), RayParams)
	if RaycastResult then
		local rotation = CFrame.new(RaycastResult.Normal, Vector3.zero)*CFrame.Angles(math.rad(90),0,0)
		local pitch = math.asin(-RaycastResult.Normal:Cross(HumanoidRootPart.CFrame.rightVector).Y);
		Root.C0 = C0*rotation
	else
		Root.C0 = C0
	end
end)
1 Like