Note: The title is wrong, and I can’t change it. For some reason it is using the question i had wanted to ask earlier, but had then solved. I meant for it to be whether you could have a force with no effect on the y axis. Sorry!
Hello! I’m working on a character movement script for a platformer game with custom gravity. I’m fairly new to coding, but I managed to make the movement script work. I had issues with the character just continuing in a direction until he hit a wall, so I added an else statement that if there was no arrow key pressed, then the character should stop moving. Here is the code:
if (Input.GetKey(Left))
{
Rb.AddForce(-gravityRight * Speed, ForceMode.VelocityChange);
}
else if (Input.GetKey(Right))
{
Rb.AddForce(gravityRight * Speed, ForceMode.VelocityChange);
}
else if (Input.GetKey(Back))
{
Rb.AddForce(-gravityForward * Speed, ForceMode.VelocityChange);
}
else if (Input.GetKey(Forward))
{
Rb.AddForce(gravityForward * Speed, ForceMode.VelocityChange);
}
else
{
Rb.velocity = Vector3.zero;
}
The only issue is that this stops vertical movement as well, so the player will fall very slowly and is unable to jump when there is no arrow key pressed. Is there a way to make it so that the stop only effects the x and z axis? Thank you!