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.
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)