Very simple Bunnyhop

Hi, I want to add simple Bunnyhop to my 3D-Game. Yet I use OnCollisionStay() and OnCollisionExit() for the Jumping, but I thought about Raycasts if it will be bugged.

Changing the forward-direction:
rb.velocity = rb.transform.forward * rb.velocity.magnitude;

Gaining Speed with Auto-Airstrafe:

if (bLastStrafeLR1 != bLastStrafeLR2) //Mouse movement changed Left/Right
{
    rb.AddForce(transform.forward * fStrafeSpeed);
}

I can gain Speed and change the direction, but with the first code Jumping/Gravity isnt working correctly. If I disable the first Code I can still Strafe, but almost not getting forward.

Question 2: What would you use for 180°-Turn of the Player when pressed Mouse Left or Right? Thank you very much.

Question 2: if-else construct that check Mouse Left or Mouse right. Then:

object.transform.Rotate(0f, 180.0f, 0.0f, Space.World);
object.transform.Rotate(0, -180.0f, 0.0f, Space.World);

About Question 1 I might have just what you need. The character in my game hops like a bunny around…

                this.gameObject.GetComponent<Rigidbody>().AddForce(transform.up * jumpPower, ForceMode.Impulse);
                this.gameObject.GetComponent<Rigidbody>().AddForce(transform.forward * jumpPower, ForceMode.Impulse);

applying two forces at the same time, one for up, one for forward at the same time, it ForceMode.Impulse that make it accelerate and jump more or less like a rabbit.

Alright, I added ForceMode.Impulse to my Bhop-Movements. But I still need this line of code:

rb.velocity = rb.transform.forward * rb.velocity.magnitude;

To change my forward-direction after one or more rb.AddForce() happened. But it almost completely disables Jumping and Gravity. So Im like floating onto the Ground and would not really fall down from something. Else it works very well.

About Rotation : Looks very good, I will test it after fixing the Problem.

Because it wipes out the .y component of velocity, setting it to zero.

Instead, copy out the velocity to a temporary variable, change the x and z components appropriately in that temp var, leaving the y component alone, and put the temp var back into the rb

EDIT: the use of rb.velocity.magnitude needs to be rethought because as you gain downward or upward velocity, that magnitude will ALSO be added, leading to extremely rapid acceleration.

Instead of pottering around with random combinations of vectors and math and magnitudes, you may wish to do a tutorial for whatever game is closest to this “bunny hop” mechanic you describe. This stuff is pretty well covered out there in Youtube land.

Imphenzia: How Did I Learn To Make Games:

1 Like