Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

AddTorque to RigidBody2D attached by SliderJoint2D

Discussion in '2D' started by Kurius, Dec 11, 2013.

  1. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    It seems like you can't AddTorque to a RigidBody2D attached by SliderJoint2D to another RigidBody2D?!

    I'm trying to make a car. I have a box shaped rigidbody that is the body of the car. Then I attach a circle shaped rigidbody to it via a slider joint. Then I try to add torque to the rigidbody in order to spin the wheel, but nothing happens!
    Alternatively everything works fine if I use a spring joint instead of a slider joint. And everything works fine if I use a hinge joint instead of a slider joint.

    So why doesn't SliderJoint2D play nicely with AddTorque?
     
  2. RadekG

    RadekG

    Joined:
    Sep 9, 2013
    Posts:
    6
    I have the same problem. I'd like to connect the wheels to the body using spring joints (for the suspension) and slider joints (so that the spring will only move up and down, and not sideways). After adding slider joints, adding torque to the wheels does nothing...
     
  3. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    I realized you have to do this...
    1) Attach a "wheel"to your car by using a Spring Joint and also a Slider Joint. But let's call this wheel the "hub".
    2) The hub will respond nicely as the suspension of your car but the hub won't rotate. So we need to put a tire on that hub and then we'll able to rotate the tire.
    3) Duplicate your hub circle body to be used as a tire. Attach the tire to the hub via a Hinge Joint. But now the tire will badly collide with the car body, so we need to make the tire exist on a different layer.
    4) Create 2 layers. Name one layer "car_body". Name the other layer "car_tires". Associate your tire GameObjects with the tire layer. Associate the rest of your car GameObjects with the car body layer. Now go to the Unity preferences and specify that these two layers should not collide.
    5) Now you can successfully AddTorque to your tire RigidBody2D. :D
     
  4. Dondosh2

    Dondosh2

    Joined:
    Dec 21, 2013
    Posts:
    5
    Hey, can you post some images of what you saying. It will be so helpful :)
    Image of hierarchy and the inspector ? It would be really awesome !
     
  5. unitylover

    unitylover

    Joined:
    Jul 6, 2013
    Posts:
    346
    Thank-you for mentioning the layer issue, I had no idea! This totally made my day. I'll post screens of my components in the morning :D
     
  6. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    One extra tip for you guys. My "hub" does NOT have a CircleCollider2D on it, but my "tire" does have a collider. Also I don't know if it matters but I made my hub half the size (radius) of my tire.
     
  7. unitylover

    unitylover

    Joined:
    Jul 6, 2013
    Posts:
    346
    Did you use the same weight for every rigidbody or use real values or what? I'm having some problems adjusting the suspension for different setups. At this point I 'm just going to assume my problem could be fixed adjusting some distances or weight values. I'll spend some more time today tinkering :)

    Thanks for your tips!
    Mike
     
  8. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    The car rigid body has a mass of 1
    The hub rigid body has a mass of 0.1
    The tire rigid body has a mass of 0.1
     
  9. Dondosh2

    Dondosh2

    Joined:
    Dec 21, 2013
    Posts:
    5
    Thanks this fix my problems !
     
  10. langem

    langem

    Joined:
    Mar 18, 2013
    Posts:
    73
    @Kurius: Thanks for the useful description. It seems that at least with Unity 4.5.2 the "hub" has to have a collider (set to trigger) to ensure that the car body is abled to rotate properly. Without an attached collider the rotation of the parent-car body seems to be fixed/locked...
     
  11. rxmarccall

    rxmarccall

    Joined:
    Oct 13, 2011
    Posts:
    353
    Would anyone be willing to upload a demo showing exactly how they've setup their car? I've been struggling for months now trying to make a decent car on physics 2D that has a suspension. Any help would be very very appreciated.
     
  12. CalaveraX

    CalaveraX

    Joined:
    Jul 11, 2013
    Posts:
    143
    I've Done a Short video on this, Showing how to setup a basic car, but i have a bug, that is i have to give negative force to the wheels in order to go forward:



    On this second video i show how to solve this negative force problem
    But i'm not sure about wich one of the two aproachs is the correct one to make a car xD



    Hope this helps!
     
  13. rxmarccall

    rxmarccall

    Joined:
    Oct 13, 2011
    Posts:
    353
    Thank you for posting thiese! Will be watching after work today.