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;
}
}