How to add friction to Unity HingeJoint?
Asked Answered
P

2

0

I attach a rigidbody to a HingeJoint in Unity.

It's attached no at the the center of an anchor of a joint.

To stop it from falling I set Use Motor = true, Target Velocity = 0 and apply some force to the motor.

However, this does not help, see video:

http://g.recordit.co/JJ0cmfT1Mb.gif

Here is the sample project: https://drive.google.com/open?id=0B8QGeF3SuAgTNnozX05wTVJGZHc

Screen to reproduce the issue (there is no code): enter image description here

How can I apply a friction to a joint to stop rigidbody from moving?

Setting spring with damper or applying a limit is not an option for me, because I need to rotate the rigidbody correctly when I apply enough torque (force) to a motor.
If I switch damper/limit on and off, the rigidbody will move faster when it is rotated in a direction of falling and slower when it is rotated in opposite direction.

Palish answered 12/6, 2017 at 10:47 Comment(3)
So you want so much friction on this joint hthat it can't fall. but you can still apply another force to move it?Rosabelle
Yes, I try to simulate excavator arm.Palish
I guess you'll want gravity on the arm for the centre of mass moving about as it extends then. You may want to turn off the motors and write your own script to apply torques to the joints. Idealy you'd use a PID controller, but just having the force be proportional to the distance from the desired angle, and angular drag on the segments should give you reasonable results.Rosabelle
R
1

I've put together a behaviour that applies friction on a hinge joint. Here it is:

using UnityEngine;

public class JointFriction : MonoBehaviour {

  [Tooltip("mulitiplier for the angular velocity for the torque to apply.")]
  public float Friction = 0.4f;

  private HingeJoint _hinge;
  private Rigidbody _thisBody;
  private Rigidbody _connectedBody;
  private Vector3 _axis;  //local space

  // Use this for initialization
  void Start () {
    _hinge = GetComponent<HingeJoint>();
    _connectedBody = _hinge.connectedBody;
    _axis = _hinge.axis;

    _thisBody = GetComponent<Rigidbody>();
  }

  // Update is called once per frame
  void FixedUpdate () {
    var angularV = _hinge.velocity;
    //Debug.Log("angularV " + angularV);
    var worldAxis = transform.TransformVector(_axis);
    var worldTorque = Friction * angularV * worldAxis;

    _thisBody.AddTorque(-worldTorque);
    _connectedBody.AddTorque(worldTorque);
  }
}

This answers the headline of your question, and is what I was looking for when I found it. However this probably won't work for what you want from your more detailed description.

This applies friction that is proportional to the angular velocity, if there's no angular velocity, no force is applied. This means that under a constant force, like gravity in your example, it will always move at some reduced speed. In reality, imperfections in materials and other real world messiness mean that applying friction to joints can keep them still against a constant torque, but in a game engine, you will need some other torque to counter your constant force.

Rosabelle answered 12/11, 2017 at 20:49 Comment(0)
S
0

It sounds like you don't want gravity. You can just turn gravity off in the rigid body's inspector.

Sturges answered 16/6, 2017 at 3:33 Comment(6)
No, I need gravity.Palish
Ok perhaps you need to clarify your goals. You said you don't want it falling.Sturges
Yes, I don't need it to fall, but I want to rotateit with constant velocity up and down. To do this, I need some friction.If there is no friction when I move it, it will rotate faster when it's moved down and slower when it's moved up.Palish
I understand that gravity tries to rotate the rigidbody, but motor should prevent that.Palish
Well it's still not clear to me what you're trying to do, but if you want to offset gravity you could calculate the torque that gravity is acting on your object and then modify the force applied by the motor to exactly offset gravity. But these calculations are not easy and because of the way unity does physics, definitely won't be exactly accurate.Sturges
You can turn of gravity for a specific object, so it doesn't fall, and still use the motor to move it up and down. This would make it move up at the same speed it moves down, because you don't have gravity added to the downward force.Rosabelle

© 2022 - 2024 — McMap. All rights reserved.