character's limb movement restrictions

Hello everybody, new Unity user here.

I have a simple character to which I assigned different keys to move different parts of the body: R for left shoulder, E for left elbow, etc, at the click of the mouse. How can I restrict those movements to keep them realistic? I don't want the knee or elbow to bend beyond what's supposed to bend. Here's the code I'm trying to tweak. It's for the left hip. Many thanks.

    var smooth:int;

private var targetPosition:Vector3;

function Update () {
    if(Input.GetKeyDown(KeyCode.F))
    {
        var playerPlane = new Plane(Vector3.left, transform.position);
        var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
        var hitdist = 0.0;

        if (playerPlane.Raycast (ray, hitdist)) {
            var targetPoint = ray.GetPoint(hitdist);
            targetPosition = ray.GetPoint(hitdist);
            var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
            transform.rotation = targetRotation;
        }
    }

    transform.position = Vector3.Lerp (transform.position, targetPosition, Time.deltaTime * smooth);
}

There are a couple of ways to go about this:

  • Easy. Use rigidbodies and setup joints to constrain the movement. Rigidbodies enable the physics system to do all of the work for you. By changing collisions, etc. and/or removing the rigidbodies when you don't need them, this system can be done with a bit less overhead. I recommend CharacterJoints. There's also a ragdoll wizard that will set your joints up for you if your object is setup correctly.
  • Medium. It seems a bit roundabout, but you could create animations on different layers for the different movements and then play the different animations a certain duration based on your input. The advantages of this implementation is that you only need to change your animations to change your restrictions and movements and this keeps you away from any sticky math. This does not however give the most natural impression without having to do some solving.
  • Medium-Hard. Constrain the values numerically (clamping localRotation or something of the sort). This will require a lot of testing and tuning as well as a fair amount of adjustment to the above code and your setup because simply Lerping one part to some mouse position will not adjust the connected parts and you'd still need to constrain both position and rotation. If you set the pivots for all of the objects to be at the point where they join their parent, then you only need to concern yourself with rotation of the joints and can ignore positions for the most part. If you want positional control (such as drag the hand to this point) with this implementation, you may have to write your own IK solver to determine the rotations to use to make that happen.

Some notes about your code:

  • Why have targetPoint and targetPosition when they're the same?
  • Why are you storing targetRotation when you never use it after the next line? If you did something else with it, this would make sense, but with the posted code, you may as well just assign your quaternion directly.
  • You actually don't need to calculate the look rotation in this case as there is a LookAt function.
  • You're checking a world-axis-left facing plane, not a camera-aligned or transform-relative oriented plane. This may not be wrong depending on your use case, but I feel it is worth mentioning to identify unexpected behaviour that this may cause if your setup is not expecting it to be world-aligned.
  • Your movement will only smooth as it approaches the target (is this desired behaviour?) and it is very framerate dependent. You might consider moving the lerp code into FixedUpdate and simply lerping by a constant fraction.

This is a shorter and clearer version of your code:

var smooth:float;
private var target : Vector3;

function Update () {
    if(Input.GetKeyDown(KeyCode.F)) {
        var playerPlane = new Plane(Vector3.left, transform.position);
        var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        var hitdist = 0.0;

        if (playerPlane.Raycast (ray, hitdist)) {
            target = ray.GetPoint(hitdist);
            transform.LookAt(target);
        }
    }
    transform.position = Vector3.Lerp(transform.position,target,
                                      Time.deltaTime*smooth);
}