#issue with wallJumpPower

1 messages · Page 1 of 1 (latest)

tropic gazelle
#

Hi, I'm new to game development and programming in general.

Right now, I'm working on a 2D platformer movement script. The last change I made was adding acceleration and deceleration to the player's horizontal (X-axis) movement.

After this change, a new problem appeared:

  • The wall jump power on the X-axis got significantly weaker,
  • And no matter how high I set the X force, the result is always the same.

For example, whether I set the X wall jump force to 1 or 100, the actual horizontal force during the wall jump feels exactly the same.

I can't tell exactly where the issue is, maybe the added acceleration logic is interfering?

Can anyone help me find where the problem might be?

Here's the full script:
https://pastebin.com/1cvsbWJ2

copper ice
# tropic gazelle Hi, I'm new to game development and programming in general. Right now, I'm work...

I believe what's happening is something like this:

Unity works as follows: (I'm sure you already know this, but just to be clear!)
Update is run every frame, so depending on hardware, it can run from 30 to 300 times per second. That's why it's generally suggested not to do physics-related logic in Update. Of course this is not a rule, but a suggestion.
FixedUpdate runs at even intervals (50 times per second by default).

In your code, you update the velocity of the player inside FixedUpdate, by doing:
rb.velocity = new Vector2(currentSpeed, rb.velocity.y); at line 112.

This only really changes the X-velocity of the player, keeping whatever Y-velocity the player had.

Inside Update you do many checks and some velocity-changing operations too.
This is generally fine, but needs to be used carefully.

Due to how Unity works, FixedUpdate always runs right before the internal physics update.
Here's Unity's own diagram of execution order. It can be extremely useful to understand.

In both FixedUpdate and Update you change the velocity using:
rb.velocity = someVector. This is usually fine, but in your case, everything you change on the X-velocity inside of Update will be overridden by FixedUpdate running your normal horizontal movement.

There's plenty of ways to fix this.
One way could be to modify the value of a private variable wallJumpImpulse inside of Update, and adding/replacing it with the X-value of your movement code inside of FixedUpdate.
Keeping changes to your rigidbody.velocity inside of FixedUpdate is good practice, and usually makes the code more clear.

Also, nice that you're keeping your script neat with regions. I can tell you care about the code you write :)