Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Car Collision Return Force

Discussion in 'Physics' started by siddharth3322, Feb 11, 2019.

  1. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,049
    As per my game requirements, I was giving manual force when two cars collide with each other and move back. So I want the correct code that can justify this. Here is the example, collision response that I want to get:

    cars_side_collision.png

    As per my understanding, I have written this code:
    Code (CSharp):
    1. Vector3 reboundDirection = Vector3.Normalize(transform.position - other.transform.position);
    2.     reboundDirection.y = 0f;
    3.     int i = 0;
    4.     while (i < 3)
    5.     {
    6.         myRigidbody.AddForce(reboundDirection * 100f, ForceMode.Force);
    7.         appliedSpeed = speed * 0.5f;
    8.         yield return new WaitForFixedUpdate();
    9.         i++;
    10.     }
    I am moving, my cars using this code:

    Code (CSharp):
    1.  //Move the player forward      
    2.     appliedSpeed += Time.deltaTime * 7f;
    3.     appliedSpeed = Mathf.Min(appliedSpeed, speed);
    4.     myRigidbody.velocity = transform.forward * appliedSpeed;
    Still, as per my observation, I was not getting, collision response in the proper direction. What is the correct way for getting above image reference collision response?
     
  2. hjohnsen

    hjohnsen

    Joined:
    Feb 4, 2018
    Posts:
    67
    You do not need any code for the effect that you want.
    If the rebound is not like you want, you can check the rigidbodies parameters (mass, drag) and also the rigibody material.I think you can find samples of standard rigibody materials in the standard assets.
     
  3. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,049
    I was looking for manual force because constantly I was applying velocity to give its a continuous movement. So when two cars collision there is no bounce back. So I require to apply something manually. I was working on this kind of game - https://www.youtube.com/results?search_query=bumper.io+game