Search Unity

Same Collision Rebound Force on Cars

Discussion in 'Physics' started by siddharth3322, Jan 9, 2019.

  1. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    663
    I was working on a car war 3d game so each car tries to hit other cars. Based on collision, I was dragging back collided cars using applying manual force.

    car_war_game.png

    On collision apply manual force to send them back. Here is the code that I have used:

    Code (CSharp):
    1. void OnCollisionEnter (Collision other)
    2. {
    3.      if (other.gameObject.CompareTag (GameConstants.TAG_ENEMY)) {
    4.          appliedSpeed = speed * 0.5f;
    5.          elapsedEngineStartWaiting = 0f;
    6.          ApplyReboundForce (other);
    7.      }
    8. }
    9.  
    10. private void ApplyReboundForce (Collision other)
    11. {
    12.      Vector3 force = transform.position - other.transform.position;
    13.      force.Normalize ();
    14.      myRigidbody.AddForce (force * 150f, ForceMode.Force);
    15. }
    Using velocity, I have driven the car:

    Code (CSharp):
    1.   elapsedEngineStartWaiting += Time.deltaTime;
    2.      if (elapsedEngineStartWaiting < 0.7f) {
    3.          return;
    4.      }
    5.  
    6.      // Move the player forward
    7. //        thisTransform.Translate(Vector3.forward * Time.deltaTime * speed, Space.Self);
    8.      appliedSpeed += Time.fixedDeltaTime * 7f;
    9.      appliedSpeed = Mathf.Min (appliedSpeed, speed);
    10.      myRigidbody.velocity = transform.forward * appliedSpeed;
    At present after the collision, car moving back behavior remain dynamic based on current car velocity and collision angle though I was applying the constant impulse to rigidbody.

    I want, collided both cars get the same amount of rebound force as per my gameplay implementation. This I can't able to achieve so I want your side some suggestions into this.