How do i reduce speed and change direction due to an object collision mid air?

I’m having a bit of trouble explaining what I wanted to do verbally so i made a picture in paint in order to explain it better:[19426-unity+air+collision+explained.png|19426]

so basically when I move and jump I get a certain speed ( depicted orange as approx. 5m/s)
when the player collides with the object he gets a new speed (blue) but as soon as the collision stops he goes back to the original (orange) speed but i have no idea how i would go about doing that since I don’t know how I would get the vector of the blue speed i tried to do something like:

Airspeed=positionthisframe-positionlastframe;
but it didnt work.

this is the code I have so far (inside Update ):

//air movements

airaccLR = Input.GetAxisRaw(“Horizontal”);

airaccFB = Input.GetAxisRaw(“Vertical”);

//vectors defined

	Vector3 jump = new Vector3(0,verticalVelocity,0);
	Vector3 groundmobility = new Vector3(sidespeed*hraði,0,forwardspeed*hraði);

	Vector3 total= new Vector3(0,0,0);
	Vector3 aircontrol = new Vector3(0,0,0);
	//avoids addition of leapspeed and groundmobility
	if(leapspeed.z !=0){
		groundmobility.z=0;
	}
	else if(leapspeed.x !=0){
		groundmobility.x=0;
	}
	//sets the ground and air rotation
	groundmobility =transform.rotation*groundmobility;
	aircontrol = transform.rotation*aircontrol;
	//ground movement
	if(characterController.isGrounded){
	airaccFB=0;
	airaccLR=0;
	groundtoair = groundmobility;
	leapspeed = transform.rotation*leapspeed;
	if(leapspeed !=new Vector3(0,0,0)){
			clamp = 17;
		}else{
			clamp = 10;
		}
			
	total = leapspeed+jump+groundmobility;
	}
	//air movement
	else{
	groundtoair += leapspeed;
	leapspeed.Set(0,0,0);
	aircontrol = new Vector3(airaccLR,0,airaccFB);
    aircontrol = transform.rotation*aircontrol;
	groundtoair += 0.2f*aircontrol;
	groundtoair = Vector3.ClampMagnitude(groundtoair,clamp);
	total = groundtoair;
	total = total + jump;
	}
	
    //speed input
	characterController.Move(total*Time.deltaTime);

sorry if the code is a little messy,
groundtoair is basically the speed in the air and is defined outside of Update(),
leapspeed is defined and calculated elsewhere but it behaves very similarly to groundtoair

Use OnControllerColliderHit() to Check when a collision occurs. Then if your character is not grounded, you can modify your vectors to match the collision constrained velocity. And you can use CharacterController.collisionFlags to check where the collision came from. You might need to store the character’s last known position so you can calculate the velocity.