# How can I add an impulse force on top of my walking force?

I want to have 3 forces in my 2D platformer game - basic left and right walking, a jump, and a gun which boosts the player away from the mouse.

The only way I have found to make my player move linearly with a constant velocity has been to do something like this:

``````float hAxis = Input.GetAxisRaw("Horizontal");
rb.velocity = new Vector2(hAxis * spd, rb.velocity.y);
``````

and my gun force code is

``````if (Input.GetMouseButtonDown(0))
{
var dir = Input.mousePosition - Camera.main.WorldToScreenPoint(transform.position);
rb.velocity = -dir.normalized * gunForce;
}
``````

However, by directly setting the x velocity of the rigidbody it overrides whatever force the gun was putting on the character, which I donâ€™t want. I would want for moving left when the gun is sending you right to slowly stop the character, and if the character was moving right and the gun is also sending you right I would want the character to either continue at the speed the gun was moving the character or to slowly move to the normal walking speed (whichever is easier to code).

Any way that I have tried to change the walking script has just made the movement exponential.

Potentially there are ways this can be simplified, but you can use Mathf.Lerp to transition the value overtime. If you add this code and then adjust your velocity code to `rb.velocity = new Vector2(hAxis * (spd + Impulse), rb.velocity.y);` Then you can achieve the effect you are looking for.

``````public bool ImpulseTrigger;
float ImpulseTimer;
public float Impulse;
float initialImpulse = 5;
float LerpInterpolator;

void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
TriggerImpulse();
}
}

private void FixedUpdate()
{
if (ImpulseTrigger)
{
ImpulseTimer -= Time.fixedDeltaTime;
if (ImpulseTimer < 0)
{
ImpulseTrigger = false;
}
LerpInterpolator += Time.deltaTime;
Impulse = Mathf.Lerp(initialImpulse, 0, LerpInterpolator);
}
}

void TriggerImpulse()
{
LerpInterpolator = 0;
ImpulseTrigger = true;
ImpulseTimer = 2;
}
``````