Hello everyone,
Im currently developing a custom character controller to allow players to walk around spherical planets. I have successfully made something like this before but I used a physical part to collide with the ground and prevent the legs from sinking. However, this time I would like to follow the standard procedure of using a force against gravity to simulate hip height.
I have the main thing working already, however there’s a problem, the equation is although able to achieve the desired hip height it is not able to maintain the constant height while walking around the sphere. Im sorry for the bad wording, english is not my native language.
Here are some examples:
In the output I have printed the distance between the root part and the ground, obtained by rayCast.Distance
.
I expected the distance from the ground to remain constant, not change with latitude and longitude.
I am not able to find any correlation between these either. The distance between the character and the ground should always be equal to the hipheight, however I am only able to achieve that with variations from .2 to -.2 studs.
Here’s my code for the hipheight force:
local t = .05
local function getUpForce(targetHeight, currentHeight, currentYVelocity)
local aUp = 196.2 + 2 * ((targetHeight - currentHeight) - currentYVelocity * t) / (t * t)
--aUp = math.max(0, aUp)
return aUp * mass
end
local h = 3.2 -- hip height
local ray = r(workspace, hrp.Position, -hrp.CFrame.UpVector * 100, rp)
if ray then
dt = math.clamp(dt, 0, .1)
--local currentY = ((hrp.Position - planetPos).Magnitude) - rad
local currentY = ray.Distance
local yVel = (currentY - lastY) / dt
lastY = currentY
--local targetY = ((ray.Position - planetPos).Magnitude + h) - rad
local targetY = h
standForce.Force = Vector3.yAxis * getUpForce(targetY, currentY, yVel)
print(ray.Distance)
lastNormal = ray.Normal
end
I am applying this force to a VectorForce relative to the humanoid, with applyAtCenterOfMass true.
You may also suggest alternative methods to achieve hipheight functionality.
Thanks.