我已經把一個在鉸鏈接頭上施加摩擦的行爲放在一起。那就是:
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);
}
}
這個回答你的問題的標題,而這也正是我一直在尋找的時候,我發現它。然而這可能不適合你想要從你的更詳細的描述。
這適用與角速度成比例的摩擦,如果沒有角速度,則不施加力。這意味着,在恆定的力量下,例如你的例子中的重力,它總是會以一定的速度移動。事實上,材料和其他現實世界混亂的不完美性意味着對關節施加摩擦可以使它們保持恆定的扭矩,但在遊戲引擎中,您需要一些其他扭矩來抵抗恆定的力量。
所以你想在這個關節上有這麼多的摩擦力,它不能掉下來。但你仍然可以運用另一種力量來移動它? – kiml42
是的,我嘗試模擬挖掘機手臂。 – Skyblade
我想你會希望重心在胳膊上移動,隨着時間的推移,質心向左移動。您可能需要關閉電機並編寫您自己的腳本以將扭矩施加到關節上。理想情況下,你會使用一個PID控制器,但只是使力與距所需角度的距離成正比,並且段上的角度阻力應該會給你合理的結果。 – kiml42