Constant Force Script Changing

I’m doing a pong type game, everything is OK, but after a certain time of playing the ball starts to get slower until almost stopping. To fix this I added a constant force to the ball, and I intended to change the force every time it hit a player.

void OnCollisionEnter(Collision myCollider){
	if(myCollider.gameObject.CompareTag("Player1")){
		constantForce.force = new Vector3(0, 5, 0);
	}
	if(myCollider.gameObject.CompareTag("Player2")){
		constantForce.force = new Vector3(0,-5,0);
	}
}

The script works fine, but only the first time, it don’t changes the force when hitting the other player. Some help please?

What are you doing with constantForce.force? To get a constant force, you must apply it at each FixedUpdate. Anyway, I suppose this is working, but the effect isn’t what you expect: when you apply a constant force to a rigidbody, it will increase its velocity at a constant rate proportional to the force. When it hits the first player, the force applied starts to accelerate the ball to the other player. When it hits the second player, the force will start to desaccelerate the ball instead of push it back immediately, what can give you the idea that it’s not working.

You could have better results if the force was applied during only one FixedUpdate cycle. A simple way to do that is to have at FixedUpdate something like this:

function FixedUpdate(){
  rigidbody.AddForce(constantForce.force);
  constantForce.force = Vector3.zero;
}

I’m supposing this code is attached to the ball.