Search Unity

  1. Unity 2019.1 beta is now available.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We're looking for insight from anyone who has experience with game testing to help us better Unity. Take our survey here. If chosen to participate you'll be entered into a sweepstake to win an Amazon gift card.
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Unity 2018.3 is now released.
    Dismiss Notice
  6. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Car Collision Return Force

Discussion in 'Physics' started by siddharth3322, Feb 11, 2019 at 12:45 PM.

  1. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    577
    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:
    33
    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:
    577
    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