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 Game Physics

Discussion in '2D' started by CalaveraX, Jul 17, 2014.

  1. CalaveraX

    CalaveraX

    Joined:
    Jul 11, 2013
    Posts:
    143
    Hi everyone!

    i'm whiling to create a game like this one



    But sadly i cant find any information about Wheel Joints 2D besides the documentation of unity.

    I handed myselft to get a simple car with the wheels, and add a motor force on input, but if the car jumps, when the wheels touch again the floor everything get broken and the car gets punched to the air like a rocket, rotation over himselft and the wheels and etc... really really broken xD

    Anyone have some information or some steps to get a car working with a wheel and a suspension?

    Ant help would be really appreciated! :)

    Thanks in advance!
     
  2. felagund18

    felagund18

    Joined:
    Jan 11, 2014
    Posts:
    2
    Are you still willing to create something like it, or have you already done it?
     
  3. CalaveraX

    CalaveraX

    Joined:
    Jul 11, 2013
    Posts:
    143
  4. danzhor

    danzhor

    Joined:
    Sep 7, 2013
    Posts:
    8
    hey, I was looking at that tutorial too...
    The funny thing about wheelJoint2d is that it shouldn't be attached to the wheel (as mentioned in that tutorial), because otherwise you have to change the angle of suspensions (as mentioned there too) and I'm not quite sure if this isn't the root cause of your problem.

    So I would suggest you to try and change it.
    Add the wheelJoint2d to the body of the car and add the wheels as connected rigidbodies in wheelJoint2d. place the wheelJoint2d as in tutorial (so the connected rigidbody and the anchor are at the same place in the middle of the wheel).
    Now you can try either to add speed to Motor of the front wheelJoint2d or to addTorque to the front wheel directly.
     
  5. CalaveraX

    CalaveraX

    Joined:
    Jul 11, 2013
    Posts:
    143
    Cool, i will try that way.

    did you make it work? cause i'm really lost with this, i have a lot of cool assets that a friend graphic designer gave to me, so i could make a game like this one, but i'm really lost :(

    Thanks!
     
  6. danzhor

    danzhor

    Joined:
    Sep 7, 2013
    Posts:
    8
    It works for me to some extent. I have a problem which is that when I jump and the car is in the air, it slowly rotates into some direction and when I stop the motor/front_wheel, it starts to rotate in different direction.

    It looks like some problem with angular drag or maybe with mass of the wheels, because if I don't change the motor speed in the air, it looks quite ok... fly in some direction and land and go on.
    I'm going to test a few different things, like changing the mass of body and wheels and maybe moving the car with adding torque to the wheel's rigidbody (instead of using motor). Will see if that will change anything. (I'm counting on the mass, to be honest:)
     
  7. CalaveraX

    CalaveraX

    Joined:
    Jul 11, 2013
    Posts:
    143
    The thing i dont quite understand (Maybe because i'm not looking at it with unity atm) is that if you attack the wheeljoint2d to the car body instead of the wheels, how do you make to add motor force to the wheels, and get the wheels to rotate when adding the motor force? (do you make this adding it the the wheeljoint attached to the car?)
     
  8. danzhor

    danzhor

    Joined:
    Sep 7, 2013
    Posts:
    8
    the thing is, that wheelJoint2d is designed in a way that the motor add torque (or speed or what) to the connected rigidbody and not the the attached gameobject (the anchor). to say it more clearly it gives the rotation to the blue full_circle, not to the blue empty_circle (blue circles, which you see when select gameObject with wheelJoint2d).

    Also that is the reason why in that tutorial it has the change of the spring angle (in wheelJoint2d). Because when you attach it to the wheel it rotates the other part, the car_body, and so you have to adjust the spring all the time :)

    At least that is how I understand that.
     
  9. CalaveraX

    CalaveraX

    Joined:
    Jul 11, 2013
    Posts:
    143
    Cool i'll check on this when i get home!

    I'll share my experience here if i get something working good :)
     
  10. CalaveraX

    CalaveraX

    Joined:
    Jul 11, 2013
    Posts:
    143
    You where rigth, The body have the 2 wheelJoint2D with CollideConnected triggered and the connected rigid body linked to both wheels, and not a single line of code and the car is working.

    i have to tune the suspension now, it feels like a car with no suspension xD and i dont know why if i add possitive force to the motor, the truck goes back, and if i add negative force it goes forward xDDD

    Thanks dude! i'll post any progress on this
     

    Attached Files:

  11. CalaveraX

    CalaveraX

    Joined:
    Jul 11, 2013
    Posts:
    143
    I've managed to get this, but i dont know why, the motor moves forwards only with negative force :(

    And the wheels are turning really fast but the car moves really slow :(

     
  12. langem

    langem

    Joined:
    Mar 18, 2013
    Posts:
    73
    try to add physics materials to the wheel collider and the ground
     
  13. CalaveraX

    CalaveraX

    Joined:
    Jul 11, 2013
    Posts:
    143
    i did, even with a material with 10000 on friction the car still doesnt move fast enough
     
  14. langem

    langem

    Joined:
    Mar 18, 2013
    Posts:
    73
    also you could apply a force with the local "right" direction of the car if your car is grounded
     
  15. CalaveraX

    CalaveraX

    Joined:
    Jul 11, 2013
    Posts:
    143
    Just checked doing addforce, and the car isnt moving, it only applies the force to the body of the car (or the wheels, tested both cases) and it just lean forward or back and stays on the same spot.

    with both forces in action (RigidBody2D.addForce) && Motor.addforce, it works really bad...

    it moves as slow as always, and the car is constantly leaning forward :(
     
  16. NRB007

    NRB007

    Joined:
    Jan 31, 2016
    Posts:
    3
    Does any one Found solution i am also stuck with this problem