Search Unity

Question HingeJoint2D prevents falling of a rigidbody2d

Discussion in '2D' started by Kraznetor, Mar 4, 2021.

  1. Kraznetor

    Kraznetor

    Joined:
    May 7, 2019
    Posts:
    23
    Hi, I'm using Hinge Joint 2D on a bike, to make it rotate around its pivot, but with this component, the body doesn't fall with gravity... how can I solve?
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    A HingeJoint2D won't affect gravity. If you've attached the HingeJoint2D to nothing i.e. the world then it obviously won't allow the object to move its position, only rotation.
     
  3. Kraznetor

    Kraznetor

    Joined:
    May 7, 2019
    Posts:
    23
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Controller : MonoBehaviour
    6. {
    7.     public float speed = 1500;
    8.     public float rotationSpeed = 150;
    9.  
    10.     public WheelJoint2D backWheel;
    11.     public HingeJoint2D body;
    12.  
    13.     private float movement = 0f;
    14.     private float rotation = 0f;
    15.  
    16.  
    17.  
    18.  
    19.     private void Update()
    20.     {
    21.         movement = Input.GetAxis("Vertical") * speed;
    22.         rotation = Input.GetAxis("Horizontal") * rotationSpeed;
    23.  
    24.     }
    25.  
    26.     private void FixedUpdate()
    27.     {
    28.  
    29.        
    30.  
    31.  
    32.         if (movement == 0f)
    33.         {
    34.             backWheel.useMotor = false;
    35.         }
    36.         else
    37.         {
    38.             JointMotor2D motor = new JointMotor2D { motorSpeed = movement, maxMotorTorque = 10000 };
    39.             backWheel.motor = motor;
    40.            
    41.  
    42.  
    43.         }
    44.  
    45.         JointMotor2D motorRotation = new JointMotor2D { motorSpeed = rotation, maxMotorTorque = 100000 };
    46.         body.motor = motorRotation;
    47.  
    48.        
    49.  
    50.  
    51.     }
    52. }
    53.  
    I tried with this because the rotation is much smoother, I even tried transform.Rotate and AddTourque, but they won't represent a good rotation physics for a bike
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    I'm not sure what you're telling me. You didn't confirm how you had the joint connected. No idea what it is you're trying to achieve apart from "I'm using Hinge Joint 2D on a bike, to make it rotate around its pivot". So on a bike and rotate around its pivot but no idea what "its pivot" is.

    If you have a HingeJoint2D on your bike and the other side connected to a point on the world then you'd expect it not to move because it's what you asked for. I ca nonly presume you're using it for a wheel so you'd have the joint on the wheel body then connect the other side to the (bike) frame body.

    I'm not sure if it'd help but there's an example monster trunk scene in my GitHub repo here that might help. This uses the WheelJoint2D though so might not be relevant.

     
    Last edited: Mar 4, 2021
  5. Kraznetor

    Kraznetor

    Joined:
    May 7, 2019
    Posts:
    23
    I'll try to explain as easy as possible, I perfectly used a script to make the bike(or in your case the truck) go forward and backward(with up and down arrows), but I can't figure out a way to make the bike rotate(with left and right arrows).
    I have a "public Rigidbody2D body" assigned to the body of the bike and then I tried to use something like:
    body.transform.Rotate(0, 0, rotation); where rotation = Input.GetAxis("Horizontal") * rotationSpeed;

    I even tried body.AddTorque(rotation); but the problem is that the rotation is not smooth, when the wheels are touching the ground, the rotation speed is fine, but when the bike is in the air, the rotation speed is insane.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    One thing at a time. The original post "HingeJoint2D prevents falling of a rigidbody2d". Now you're talking about rotation. Have you moved on to another issue and assume I know?
     
  7. Kraznetor

    Kraznetor

    Joined:
    May 7, 2019
    Posts:
    23
    I said that badly, the point of the question is about rotation of a bike, I wanted to try to rotate it with a HingeJoint, but as you said "A HingeJoint2D won't affect gravity" so I stopped using it and tried body.transform.Rotate(0, 0, rotation) or body.AddTorque.
    I added maxAngularVelocity in order to keep the rotation of the body slower:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Controller : MonoBehaviour
    6. {
    7.     public float speed = 1500;
    8.     public float rotationSpeed = 150;
    9.     public float maxAngularVelocity = 150;
    10.  
    11.     public WheelJoint2D backWheel;
    12.     public Rigidbody2D body;
    13.  
    14.     private float movement = 0f;
    15.     private float rotation = 0f;
    16.  
    17.  
    18.  
    19.  
    20.     private void Update()
    21.     {
    22.         movement = Input.GetAxis("Vertical") * speed;
    23.         rotation = Input.GetAxis("Horizontal") * rotationSpeed;
    24.  
    25.     }
    26.  
    27.     private void FixedUpdate()
    28.     {
    29.         if (movement == 0f)
    30.         {
    31.             backWheel.useMotor = false;
    32.         }
    33.         else
    34.         {
    35.             JointMotor2D motor = new JointMotor2D { motorSpeed = movement, maxMotorTorque = 10 };
    36.             backWheel.motor = motor;
    37.            
    38.  
    39.  
    40.         }
    41.  
    42.      
    43.         body.transform.Rotate(0, 0, -rotation);
    44.                                                                             //body.AddTorque(-rotation);
    45.        
    46.  
    47.        
    48.         if (body.angularVelocity < -maxAngularVelocity) { body.angularVelocity = -maxAngularVelocity; }
    49.         if (body.angularVelocity > maxAngularVelocity) { body.angularVelocity = maxAngularVelocity; }
    50.  
    51.  
    52.     }
    53.  
    54.    
    55. }
    56.  
    but now the problem is that the bike is shaking, even if I try to modify the value of speed, rotationspeed, maxAngularVelocity, mass of the objects
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    Still confused here. Me saying that was to indicate it won't affect/stop gravity so not sure why that means you stop using it.

    This has nothing to do with the body. You're just using the Rigidbody2D component to grab the local Transform component (same as just referencing "transform.Rotate") and modifying the Transform. You should NEVER modify the Transform when using 2D physics components. When you add a Rigidbody2D you are asking it to do that which should be clear as otherwise how would it be able to update the position/rotation?

    Always go through the Rigidbody2D API when using physics for any kind of movement. If you add torque then that'll rotate the body by adding the torque onto the angular velocity.

    Without seeing a video, the shaking is likely the fact that modifying the position or rotation directly by modifying the Transform. Doing this instantly changes the position/rotation and causes overlaps that the solver has to then solve.

    You can turn on interpolation on the body to visually smooth Transform changes, assuming you're not stomping on them yourself.