I want the board to properly align with the player’s hitbox, as shown:
(video demonstration reverted to a version where angle calculation was not added)
But when adding angle calculation for the board, it starts freaking out:
I’m not very good at math, so I’m not totally sure what could be causing this. Any help is appreciated.
Here’s the code for updating the board angle
rs.RenderStepped:Connect(function(delta) --updates board angle
local boardAngle = skateModule.getBoardAngle(testingBoard,testingBoard.FrontEnd,testingBoard.BackEnd)
if boardAngle then
testingBoard.CFrame = hitbox.CFrame*CFrame.new(0,5,0)*CFrame.Angles(math.rad(boardAngle.X),math.rad(boardAngle.Y),math.rad(boardAngle.Z))
else
testingBoard.CFrame = hitbox.CFrame*CFrame.new(0,5,0)
end
end)
And heres the code for calculating the board angle
function skateAnimations.getBoardAngle(board, frontPoint, backPoint)
local boardRayParams = RaycastParams.new()
boardRayParams.FilterType = Enum.RaycastFilterType.Exclude
boardRayParams.IgnoreWater = true
boardRayParams.FilterDescendantsInstances={CHARACTER, frontPoint, backPoint, board, workspace.testing}
local raycastDistance = 20 --for testing, standardize later
local raycastDirection = -board.CFrame.UpVector.Unit
local function getGroundVector(position)
local raycastResult = workspace:Raycast(position + Vector3.new(0,5,0), raycastDirection * raycastDistance, boardRayParams)
makeRayVisible(raycastResult,position + Vector3.new(0,5,0),true)
if raycastResult then
return raycastResult.Position
end
return nil
end
local backGroundVector = getGroundVector(backPoint.WorldCFrame.Position)
local frontGroundVector = getGroundVector(frontPoint.WorldCFrame.Position)
if frontGroundVector and backGroundVector then
local midPoint = (backGroundVector + frontGroundVector) / 2
local direction = (frontGroundVector - backGroundVector).Unit
local midpointRay = workspace:Raycast(midPoint + Vector3.new(0,1,0), raycastDirection * raycastDistance, boardRayParams)
makeRayVisible(midpointRay,midPoint + Vector3.new(0,5,0),true,0.2,Color3.new(0, 0.333333, 0.498039))
local normal = midpointRay and midpointRay.Normal or Vector3.new(0,1,0)
local right = direction:Cross(normal).Unit
local up = normal.Unit
local forward = right:Cross(up).Unit
local goalCFrame = CFrame.fromMatrix(midPoint, right, up)
oriPart.CFrame = goalCFrame
return oriPart.Orientation
else
warn('missing ground vector')
end
end