- Home /
Always Get Same Physics Collision Rebound Force
I was working on a 3d car war game, and I was moving my cars through velocity using the following code:
appliedSpeed += Time.fixedDeltaTime * 8f;
appliedSpeed = Mathf.Min (appliedSpeed, speed);
myRigidbody.velocity = transform.forward * appliedSpeed;
At collision time, I was giving backward force using the following code:
private void ApplyReboundForce (Collision other)
{
Vector3 reboundDirection = other.contacts [0].point - transform.position;
reboundDirection = -reboundDirection.normalized;
// myRigidbody.AddForce (reboundDirection * 7f, ForceMode.Impulse);
myRigidbody.AddForce (reboundDirection * 180f, ForceMode.Force);
}
But at present, I was getting a collision response force dynamically. Sometimes it was a higher force and sometimes it was lower force based on physics calculation. I want collision response force always same.
Here is the reference game for collision response force:
SUPER ADDICTIVE NEW .io Game | Bumper.io
I have already made all physics property of all cars same like physics material, rigidbody properties values etc... then also getting different collision response on each collision.
Please give me some suggestion to achieve something similar.
Answer by xxmariofer · Jan 20, 2019 at 08:08 PM
Your code seems fine, can you try some things? When are you applying the force on collidion enter or on collision stay since that may be incrementing the applied force. Also i would suggest setting the rigidbody velocity to 0 just before you give the rigidbody the force, that will make the forces (if they all have same properties) the same.
private void ApplyReboundForce (Collision other)
{
Vector3 reboundDirection = other.contacts [0].point - transform.position;
reboundDirection = -reboundDirection.normalized;
myRigidbody.velocity = Vector3.zero;
myRigidbody.AddForce (reboundDirection * 180f, ForceMode.Force);
}
$$anonymous$$aking rigidbody velocity didn't work for me. I was applying rebound force like this way:
void OnCollisionEnter (Collision other)
{
if (other.gameObject.CompareTag (GameConstants.TAG_ENE$$anonymous$$Y) || other.gameObject.CompareTag (GameConstants.TAG_PLAYER)) {
// myRigidbody.velocity = -transform.forward * appliedSpeed;
//appliedSpeed = speed * 0.5f;
elapsedEngineStartWaiting = 0f;
ApplyReboundForce (other);
if (other.transform == targetPlayer)
AssignTargetPlayer ();
lastCollidedEnemy = other.transform;
}
}
private void ApplyReboundForce (Collision other)
{
//myRigidbody.velocity = Vector3.zero;
Vector3 reboundDirection = Vector3.Normalize(transform.position - other.transform.position);
reboundDirection.y = 0f;
//Vector3 reboundDirection = other.contacts [0].point - transform.position;
//reboundDirection = -reboundDirection.normalized;
//myRigidbody.AddForce (reboundDirection * 5f, Force$$anonymous$$ode.Impulse);
myRigidbody.AddForce (reboundDirection * 180f, Force$$anonymous$$ode.Force);
}
For more information please read this post that I have created: Always Get Same Physics Collision Rebound Force