- Home /
Same Collision Rebound Force on Cars
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.
On collision apply manual force to send them back. Here is the code that I have used:
void OnCollisionEnter (Collision other)
{
if (other.gameObject.CompareTag (GameConstants.TAG_ENEMY)) {
appliedSpeed = speed * 0.5f;
elapsedEngineStartWaiting = 0f;
ApplyReboundForce (other);
}
}
private void ApplyReboundForce (Collision other)
{
Vector3 force = transform.position - other.transform.position;
force.Normalize ();
myRigidbody.AddForce (force * 150f, ForceMode.Force);
}
Using velocity, I have drive the car:
elapsedEngineStartWaiting += Time.deltaTime;
if (elapsedEngineStartWaiting < 0.7f) {
return;
}
// Move the player forward
// thisTransform.Translate(Vector3.forward * Time.deltaTime * speed, Space.Self);
appliedSpeed += Time.fixedDeltaTime * 7f;
appliedSpeed = Mathf.Min (appliedSpeed, speed);
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.
Your answer
Follow this Question
Related Questions
Unity3D: Get Velocity after collision 0 Answers
Move Player Car in Forward Direction using Physics Force 1 Answer
Manually Apply Cars Collision Response Force 0 Answers
Character Controller meets Rigidbody 1 Answer
Calculate velocity to set to reach a target point on a plane considering the drag. 0 Answers