# Character Controller and Reverse Gravity

So I have a Game object with Rigid Body, a Character controller and my Movement script. On a specific condition I am reversing gravity. The problem is that the velocity.Y when the gravity is reversed, keeps increasing to the infinity. If I try to reset it like in the first if statement (which works for normal gravity) the object keeps bouncing from the ground. Gravity scale is either 1 or -1 in order to reverse gravity and movement.

This is the code:

 void Update()
{

_isGrounded = Physics.CheckSphere(groundCheck.position, 0.4f, whatIsGround);
Move();
}

void Move(){
//Reseting States for new cycle , unless the object is falling
if ((_isGrounded && velocity.y < 0))
{
velocity.y = -2f;
_isRunning = false;
_isJumping = false;
_isFalling = false;
}else if (velocity.y < 0 && !_isGrounded)
{
_isFalling = true;
}
//Moving on X axis
float x = Input.GetAxis(KeyInputMove);
if (x != 0)
{
_isRunning = true;
}
moveDirection = gravityScale*_transform.right * x;
_controller.Move(moveDirection * moveSpeed * Time.deltaTime);

//Perfoming jump
if (Input.GetButtonDown(KeyInputJump) && _isGrounded)
{
velocity.y = (float) Math.Sqrt(1 * -2f * globalGravity);
_isJumping = true;
}

velocity.y += gravityScale * globalGravity * Time.deltaTime;
_controller.Move(velocity * Time.deltaTime);
//Updating animation states
_animator.SetBool("Grounded",_isGrounded);//^This is the idle state
_animator.SetBool("Running",_isRunning);
_animator.SetBool("Jumping",_isJumping);
_animator.SetBool("Falling",_isFalling);

}

private void OnTriggerExit(Collider other)
{
//Reverse Gravity and Model Rotation
if (other.gameObject.tag == "Portal")
{
_transform.Rotate(0,0,gravityScale*180);
gravityScale *= -1;
}
}


The problem is that the velocity.Y when the gravity is reversed , keeps increasing to the infinity.

That's what your code tells it to do:

velocity.y += gravityScale * globalGravity * Time.deltaTime;

Perhaps you are forgetting to check if the gravity is negative or positive when determining whether the object is falling. Here's what you probably want:


if (gravityScale > 0 && velocity.y < 0)
|| (gravityScale < 0 && velocity.y > 0))
{
_isFalling = true;
} else {
_isFalling = false;
}

if (_isGrounded && _isFalling) {
velocity.y = -2f * gravityScale; //maybe you should just set it to 0?
_isRunning = false;
_isJumping = false;
_isFalling = false;
}


I assume that gravityScale is 1 when gravity is normal (objects fall downward) and -1 when gravity is reversed (objects fall upward?). I'm not really sure why you are setting the velocity to -2 in the first if condition; you might want to try setting it to 0.

• Yes this is exactly how gravityScale works. The velocity setting (-2 or 0) doesnt really matter. I followed Brackeys tutorial and he set it to -2 which works and doesnt really affect. This condition resets the Velocity and stays stable. I tried your code and it doesnt work. Also it doesnt make sense , since the object cant be _isGrounded and isFalling at the same time. I agree that the velocity.y += gravityScale * globalGravity * Time.deltaTime; is the line making the increment , but this is why we use the if to keep the velocity stable when the player isnt jumping or falling. Nov 20, 2020 at 23:08
• Have a look to the inspector in the velocity values , when above and when bellow. drive.google.com/file/d/1m9P50ONdOp5fepuQitC7V4Ym2zE_6yPS/… Nov 20, 2020 at 23:15
• @ThanosLoupas You're right, I had a couple of mistakes. Try it now. Nov 20, 2020 at 23:30
• I did and this is the result : drive.google.com/file/d/1PVIiBqLCgmqM9rAPt2QlX7DvkHE-hPSg/… Note that I am not pressing jump , it just happens. This is the bouncing effect I stated at first. Nov 20, 2020 at 23:55
• @ThanosLoupas Please add the code that sets _isGrounded to your question. Nov 21, 2020 at 0:07