So, I’m making a sonic game and to do so i have to make the player rotate to the slope they’re standing on, but when the script was made it has only regular rigs in mind. (i, e Robloxians, any character using Motor6Ds) But I’ve moved to using Skinned Meshes, and they use Bones instead, thus breaking the script. Bones don’t have C0s, so how would i go about rewriting this?
local RunService = game:GetService("RunService")
local Character = script.Parent
local HumanoidRootPart:BasePart = Character:WaitForChild("HumanoidRootPart")
local RootJoint = HumanoidRootPart:WaitForChild("Root")
local RootJointC0 = RootJoint.C0
local RayParams = RaycastParams.new()
RayParams.FilterDescendantsInstances = {Character}
RunService.RenderStepped:Connect(function(deltaTime)
local RaycastResult = workspace:Raycast(HumanoidRootPart.Position,-Vector3.yAxis*5, RayParams)
local C0Offset = CFrame.identity
if RaycastResult then
local upVector = RaycastResult.Normal
local rightVector = HumanoidRootPart.CFrame.RightVector
local backVector = -HumanoidRootPart.CFrame.LookVector
local calculationForX = CFrame.fromMatrix(Vector3.zero,rightVector,upVector)
local calculationForZ = CFrame.fromMatrix(Vector3.zero,backVector,upVector)
local x = calculationForX:ToOrientation()
local z = calculationForZ:ToOrientation()
C0Offset = CFrame.Angles(x,0,z)
end
RootJoint.C0 = RootJoint.C0:Lerp(C0Offset*RootJointC0,deltaTime*10)
end)
Im not sure if this would work with your current solution since C0 and Transform behave differently, but try:
local RunService = game:GetService("RunService")
local Character = script.Parent
local HumanoidRootPart:BasePart = Character:WaitForChild("HumanoidRootPart")
local RootJoint = HumanoidRootPart:WaitForChild("Root")
local RootJointC0 = RootJoint.C0
local RayParams = RaycastParams.new()
RayParams.FilterDescendantsInstances = {Character}
RunService.PreSimulation:Connect(function(deltaTime)
local RaycastResult = workspace:Raycast(HumanoidRootPart.Position,-Vector3.yAxis*5, RayParams)
local TransformOffset = CFrame.identity
if RaycastResult then
local upVector = RaycastResult.Normal
local rightVector = HumanoidRootPart.CFrame.RightVector
local backVector = -HumanoidRootPart.CFrame.LookVector
local calculationForX = CFrame.fromMatrix(Vector3.zero,rightVector,upVector)
local calculationForZ = CFrame.fromMatrix(Vector3.zero,backVector,upVector)
local x = calculationForX:ToOrientation()
local z = calculationForZ:ToOrientation()
TransformOffset = CFrame.Angles(x,0,z)
end
RootJoint.Transform = RootJoint.Transform:Lerp(C0Offset*RootJointC0,deltaTime*10)
end)
Transform is the only way to really move bones so youre gonna need it to work w/ that
Oh my bad i forgot smth. Here. Ive tested this and it works, I also suggest you offset the point the ray comes from to the feet so that standing on ledges doesnt make you go completely sideways.
local RunService = game:GetService("RunService")
local Character = script.Parent
local HumanoidRootPart:BasePart = Character:WaitForChild("HumanoidRootPart")
local RootJoint = HumanoidRootPart:WaitForChild("Root")
local RayParams = RaycastParams.new()
RayParams.FilterDescendantsInstances = {Character}
local TransformOffset = CFrame.identity
RunService.PreSimulation:Connect(function(deltaTime)
local RaycastResult = workspace:Raycast(HumanoidRootPart.Position,-Vector3.yAxis*5, RayParams)
local target = CFrame.identity
if RaycastResult then
local upVector = RaycastResult.Normal
local rightVector = -HumanoidRootPart.CFrame.RightVector
local backVector = -HumanoidRootPart.CFrame.LookVector
local calculationForX = CFrame.fromMatrix(Vector3.zero,rightVector,upVector)
local calculationForZ = CFrame.fromMatrix(Vector3.zero,backVector,upVector)
local x = calculationForX:ToOrientation()
local z = calculationForZ:ToOrientation()
target = CFrame.Angles(x,z,0)
end
print(TransformOffset)
TransformOffset = TransformOffset:Lerp(target,deltaTime*10)
RootJoint.Transform *= TransformOffset
end)
Also forgot to mention that if a animation isnt playing this will bug so make sure theres always a animation playing or create another loop on RunService.PreAnimation that sets the transform to CFrame.identitiy.