I am trying to develop a platformer where the player can manipulate how they experience gravity with button presses. For example when pressing W their gravity becomes up and if they press D they are pulled to the right side of the screen. For changing gravity between up and down, I just change the rigidbody’s gravity scale from 1 to -1. When changing between left and right I set the gravityscale to 0 and use a ConstantForce2D to apply a constant force equal to gravity to either the right or left. The problem I am running into is that whenever I apply these horizontal forces, the velocity of my player is immediately cancelled out. So in the case that the player is falling but then changes their gravity to the right, instead of keeping their current vertical velocity and accelerating right as I would like to happen, they suddenly stop falling and begin moving right. Is there any way to get around this? I’ve pasted the relevant code below.
private void Lash()
{
Vector2 currentVelocity = myRigidBody.velocity;
//if (!isLashing)
//{
// myRigidBody.gravityScale = 1f;
// myConstantForce2D.force = new Vector2(0f, 0f);
//}
if (!Input.GetKey(KeyCode.LeftShift))
{
if (Input.GetKeyDown(KeyCode.W))
{
myConstantForce2D.force = Vector2.zero;
isHoriz = false;
isLashing = true;
if (floor)
{
myRigidBody.gravityScale = -1f;
myAnimator.SetTrigger("floorToCeiling");
}
else if (ceiling)
{
return;
}
else if (rightWall)
{
myRigidBody.gravityScale = -1f;
myConstantForce2D.force = Vector2.zero;
Debug.Log(myConstantForce2D.force);
myAnimator.SetTrigger("rightToCeiling");
}
else if (leftWall)
{
myRigidBody.gravityScale = -1f;
myConstantForce2D.force = Vector2.zero;
myAnimator.SetTrigger("leftToCeiling");
}
}
else if (Input.GetKeyDown(KeyCode.S))
{
isLashing = false;
isHoriz = false;
if (floor)
{
return;
}
else
{
CancelLashings();
}
}
else if (Input.GetKeyDown(KeyCode.D))
{
if (floor)
{
isLashing = true;
isHoriz = true;
myConstantForce2D.force = Vector2.right * (myRigidBody.mass * 9.81f);
myAnimator.SetTrigger("floorToRight");
myRigidBody.gravityScale = 0f;
myRigidBody.velocity = currentVelocity;
}
else if (ceiling)
{
isLashing = true;
isHoriz = true;
myConstantForce2D.force = Vector2.right * (myRigidBody.mass * 9.81f);
myAnimator.SetTrigger("ceilingToRight");
myRigidBody.gravityScale = 0f;
myRigidBody.velocity = currentVelocity;
}
else if (rightWall)
{
isLashing = true;
isHoriz = true;
return;
}
else if (leftWall)
{
CancelLashings();
}
}
else if (Input.GetKeyDown(KeyCode.A))
{
if (floor)
{
isLashing = true;
isHoriz = true;
myConstantForce2D.force = Vector2.left * (myRigidBody.mass * 9.81f);
myAnimator.SetTrigger("floorToLeft");
myRigidBody.gravityScale = 0f;
myRigidBody.velocity = currentVelocity;
}
else if (ceiling)
{
isLashing = true;
isHoriz = true;
myConstantForce2D.force = Vector2.left * (myRigidBody.mass * 9.81f);
myAnimator.SetTrigger("ceilingToLeft");
myRigidBody.gravityScale = 0f;
myRigidBody.velocity = currentVelocity;
}
else if (rightWall)
{
CancelLashings();
}
else if (leftWall)
{
isLashing = true;
isHoriz = true;
return;
}
}
else if (Input.GetKeyDown(KeyCode.Q))
{
CancelLashings();
}
}
}
The animator triggers are for rotating the player to be properly oriented with the direction of gravity. ‘Lashing’ Is the name of the magic system I am implementing in this game. Any advice would be greatly appreciated.