Whenever I use transform.Translate(), the player slightly clips through a square while they fall, but rigidbody.AddForce() does not present the same issue. When I use AddForce, however, there is no regulation of the player’s speed. Is there any way I can have smooth collisions and not have acceleration?
(The player has a BoxCollider2D and Rigidbody2D, and the square has a BoxCollider2D.)
You shouldn’t use transform.Translate with a rigidbody unless you don’t expect a collision to happen. AddForce will give you acceleration, but rigidbody.velocity is what you are after. I dealt with a very similar situation and had to come up with an algorithm that tested the square magnitude of the velocity vector ( rigidbody.velocity.sqrMagnitude ) against a max and min value to find the happy medium, but while still using AddForce because it still produced a realistic movement.
rigidbody.velocity will produce the value that you declare in the vector immediately, which will skip the acceleration that AddForce applies. In my situation, I found that it was beneficial to use AddForce with a super fast acceleration initially until it made it to my happy speed at which time I regulated the speed by doing something similar to this:
allowableVariance = 1.5f;
curSqrMag = rigidbody.velocity.sqrMagnitude;
maxSqrMag = 800;
minSqrMag = 100;
speed = 10;
diff = Mathf.Abs( rigidbody.position.x - targetPos.x );
if( curSqrMag < maxSqrMag
|| curSqrMag <= minSqrMag )
{
rigidbody.AddForce ( diff * speed );
}
else{
brakeFactor = -10;
rigidbody.AddForce( brakeFactor * rigidbody.velocity );
}
Hope this helps