I’m trying to create a movement system that aligns the player to the terrain
I’ve accomplished aligning the player to the terrain by using raycasts to detect positions and then using CFrame.fromMatrix using vectors generated from those positions
What I can’t for the life of me figure out is how to apply limits to the tilt of the CFrame to prevent the player from, for example, tilting to align with a completely vertical wall. I know how to align the character to the terrain, but I can’t figure out how to limit the steepness of terrain that they can align to
Here’s the code I use to create a CFrame to align the player to the terrain under them. ‘PlatformMatrix’ contains the results of the raycasts that were sent to find points underneath the player, and contains the positions found for front/back/left/right/center, which I then use to generate the lookvector and rightvector
function GroundSensor.getAlignment(RootCFrame: CFrame, PlatformMatrix: {[string]: Vector3}): CFrame
local alignLookVector = nil
local forePos = PlatformMatrix.Front or PlatformMatrix.Center
local backPos = PlatformMatrix.Back or PlatformMatrix.Center
if forePos ~= backPos then
alignLookVector = (forePos - backPos).unit
-- My attempt at limiting how severely the player tilts front-back which does not work
if math.abs(alignLookVector.Y) > FORE_LIMIT_UP then
alignLookVector = Vector3.new(alignLookVector.X, math.clamp(alignLookVector.Y, FORE_LIMIT_LOW, FORE_LIMIT_UP), alignLookVector.Z)
end
end
local alignRightVector = nil
local leftPos = PlatformMatrix.Left or PlatformMatrix.Center
local rightPos = PlatformMatrix.Right or PlatformMatrix.Center
if leftPos ~= rightPos then
alignRightVector = (leftPos - rightPos).unit
-- My attempt at limiting how severely the player tilts left-right which does not work
if math.abs(alignRightVector.Y) > SIDE_LIMIT_UP then
alignRightVector = Vector3.new(alignRightVector.X, math.clamp(alignRightVector.Y, SIDE_LIMIT_LOW, SIDE_LIMIT_UP), alignRightVector.Z)
end
end
if not alignLookVector or not alignRightVector then
alignLookVector = -RootCFrame.LookVector
alignRightVector = RootCFrame.RightVector
end
local TerrainAlignment = CFrame.fromMatrix(RootCFrame.Position, alignRightVector, alignLookVector:Cross(alignRightVector), -alignLookVector)
return TerrainAlignment, alignLookVector.Y
end
However on certain terrain it’s possible for the player to wiggle in such a way that this can happen. I don’t want the player to be able to tilt to the terrain at these extremes, but I can’t figure out how to do that
In the image, the colored squares at the characters’ feet represent where the raycasts hit which is used to generate the raycasts used to orientate the character.
Thank you very much for any help