Ok, this is seemingly an extremely simple question. Whenever I apply Y movement (gravity) to a charactercontroller, the expected behavior happens and the object falls down until it hits the ground. The issue is when I stop applying gravity, the CharacterController constantly fluctuates between isGrounded = false and isGroudned = true. I’ve stripped down literally everything except for a few lines of code to test with:
void Update
{
Debug.Log( "is grounded: " + controller.isGrounded );
moveDirection.y -= gravity * Time.deltaTime;
controller.Move( moveDirection * Time.deltaTime );
}
In the code above, once the charactercontroller falls to the ground, isGrounded keeps returning true as expected. When I try this code…
Debug.Log( "is grounded: " + controller.isGrounded );
if( !controller.isGrounded )
{
moveDirection.y -= gravity * Time.deltaTime;
if( moveDirection.y < -maxFallSpeed ) moveDirection.y = -maxFallSpeed;
}
else moveDirection.y = 0;
controller.Move( moveDirection * Time.deltaTime );
…the controller fluctuates isGrounded values every few frames.
How do I keep the controller grounded without having to constantly apply gravity? Once it’s grounded, shouldn’t it stay grounded until I apply movement in the opposite direction? Thanks.
I guess I just don't understand that in the above code, if the only way for gravity to be suspended is if it is grounded, how is it ever becoming ungrounded without every applying an upward force? I'm trying to suspend gravity because of issues related to the charactercontroller and moving platforms (this is a 2d game). It falls through a vertically moving platform if no horizontal movement is applied to the CC and I suspect the constant gravity coupled with the upwards movement of the platform is causing an issue sometimes.
– heliosAh! I have no idea why this never occurred to me.. I'm just adding a very very slight displacement right before the main Move and all my troubles are gone. Thank you!
– heliosI've actually realized that this still fails if the platform is moving fast enough vertically and the character is falling down at the same time. Is there anything that can be done in situations like this? Thanks!
– heliosI encountered this and had a slightly different solve in my own code - instead of displacing in the X or Z directions I actually apply a very small amount of downward push (basically, instead of no gravity I add -0.1*Time.deltaTime gravity - very small). Seems to work perfectly fine for me.
– PhobicGunnerIt seems like gravity is the problem for me, though. The downward force on the player in combination with the upward force of the platform seems to have a breaking point. I just can't believe there isn't a real solution to this. Even the Unity 2D Lerp tutorials moving platforms break once they move fast enough.
– helios