# Quick question about a small pattern used in the code

The initial SoyBoyController.cs FixedUpdate():

``````void FixedUpdate() {
var acceleration = accel;
var xVelocity = 0f;

if (input.x == 0) {
xVelocity = 0f;
}else {
xVelocity = rb.velocity.x;
}

rb.AddForce(new Vector2(((input.x * speed) - rb.velocity.x) * acceleration, 0));

rb.velocity = new Vector2(xVelocity, rb.velocity.y);
}
``````

This code is responsible for moving SoyBoy in a X axis.

I usually like to refactor stuff around to get better understanding of whats going on.

I come up with something like this:

``````void FixedUpdate() {
float acceleration = accel;

rb.velocity = SetVelocity();
Vector2 force = new Vector2((input.x * speed) - rb.velocity.x, 0);
}

Vector2 SetVelocity() {
Vector2 stopVelocity = new Vector2(0, rb.velocity.y);
Vector2 currentVelocity = rb.velocity;
return input.x == 0 ? stopVelocity : currentVelocity;
}
``````

For me it is much easier too see whats going on here.

There is this code I find very interesting:

`(input.x * speed) - rb.velocity.x`

It is a part of the force calculation. It will cap your speed at a given speed when you continue pressing direction button and very quickly stop you if you stop pressing it. For such a short equation I find it very smart.

Does this operation have any ‘formal’ name?

Hey @kocur4d!

What that piece of code does is called inertia damping, which means it cancels out the energy added and removes any undesired velocity.

Cheers!

@blackdragonbe nice, exactly what I was looking for. Thank you!

I have learnt new term today!