- Home /
Set limits of joint's movement in relation to connected rigidbody
I created a humanoid character in Unity using Ragdoll Wizzard and now I am trying to set the properties of each CharacterJoint. To each joint I also added following script:
using UnityEngine;
using System.Collections;
public class MoveJoint : MonoBehaviour {
public float minXTiltAngle = 10.0F;
public float maxXTiltAngle = 10.0F;
public float minYTiltAngle = 10.0F;
public float maxYTiltAngle = 10.0F;
public float minZTiltAngle = 10.0F;
public float maxZTiltAngle = 10.0F;
public float smooth = 10.0F;
Transform jointTransform = null;
private float initialXRotation;
private float initialYRotation;
private float initialZRotation;
// Use this for initialization
public void Start () {
CharacterJoint characterJoint = gameObject.GetComponent( typeof(CharacterJoint) ) as CharacterJoint;
if (characterJoint != null) {
jointTransform = characterJoint.transform;
initialXRotation = jointTransform.eulerAngles.x;
initialYRotation = jointTransform.eulerAngles.y;
initialZRotation = jointTransform.eulerAngles.z;
}
}
// Update is called once per frame
public void Update () {
float tiltAroundX = 0.0F;//initialXRotation;
float tiltAroundY = 0.0F;//initialYRotation;
float tiltAroundZ = 0.0F;//initialZRotation;
bool move = false;
if (Input.GetAxis("Vertical") != 0.0F) {
tiltAroundX = Input.GetAxis("Vertical");
if (tiltAroundX <= 0) tiltAroundX = tiltAroundX * minXTiltAngle;
else tiltAroundX = tiltAroundX * maxXTiltAngle;
move = true;
}
if (Input.GetKey("e")) {
tiltAroundY = 1.0F * maxYTiltAngle;
move = true;
} else if (Input.GetKey("q")) {
tiltAroundY = -1.0F * minYTiltAngle;
move = true;
}
if (Input.GetAxis("Horizontal") != 0.0F) {
tiltAroundZ = Input.GetAxis("Horizontal");
if (tiltAroundZ <= 0) tiltAroundZ = tiltAroundZ * minZTiltAngle;
else tiltAroundZ = tiltAroundZ * maxZTiltAngle;
move = true;
}
if(move) MoveWithKey(tiltAroundX, tiltAroundY, tiltAroundZ);
}
void MoveWithKey(float tiltAroundX, float tiltAroundY, float tiltAroundZ){
Quaternion target = Quaternion.Euler(initialXRotation + tiltAroundX,
initialYRotation + tiltAroundY, initialZRotation + tiltAroundZ);
jointTransform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth);
}
}
The script just takes input from the user and applies move to the joint (respecting initial rotation). Initial angles are set to 10, but I changed them for every joint in Unity. And here comes my question. How can I set some limits of joint's movement in relation to rigidbody to which it is attached? Below I give screenshots, which may explain what I mean.
How should the leg look, when limits applied: What I get now:
Answer by erofes · Apr 23, 2021 at 09:00 AM
Hi! Probably you should use joint.swing1Limit, joint.swing2Limit, joint.lowTwistLimit and joint.highTwistLimit to limit angle rotation on each axis