Walking Quarduped with physics

I’m trying to make a quadruped that moves with in-game physics. At the moment my plan is to use legs on joints, which work by lifting up, rotating towards the destination, extending and returning to original position. Rear legs would do the same without sideways movement, so the creature would automatically turn to face the direction its moving.

Apparently the customizable joints should be best for the wobbly movement I’m looking for, but I have no idea how to make them rotate. I have successfully made a leg spin, but how do I simply rotate the limb from one specific position to another?

I’m going about this wrong or unoptimally, suggestions for better methods of making this are welcome too.

The first thing you need to realize about getting this to work with physics is that using “AddForce” behaves like a thruster… there is no reactive force. So as the leg presses down against the terrain, it will stop and won’t “push away” from the terrain which elevates the body of the walker. You can move the bones or joints a few different ways. You can use translate (which doesn’t really use physics), you can use “AddForce” which applies a force and moves a rigidbody in the direction specified, then you can also use joint motor (which I still don’t understand very well).

Have a look at this thread…

I was trying to do something semi-similar except I was looking to model the outriggers on a mobile crane. Uberpete shared a method of implementing the joint motion using a spring motor, or joint motor. The other thing to keep in mind is that you may need to lock the joints at some point and you can’t use the “Freeze Position” property because it’ll lock the model up and not allow it to move. You also can’t just lock that movement direction because it will return the joint to its original position. So it seems the best way to lock the joint is to create (then destroy) a fixed joint. You also might be able to do this with Inverse Kinematics… but I haven’t had much experience with it. Check out this asset store plug-in:

The creator of that plug in used it to create a quadruped, so it may be just what you’re looking for and may save you from having to re-invent a wheel.

Thanks for the response. I’m using joints to move the rigidbody limbs, which at least seems to work when the leg is just spinning around. The problem is, I still haven’t found out how to control the joint target rotation from the script.

I’m also starting to consider using an extra part for elbows to get one hingejoint for back and forth movement and another for sideways movement, because I don’t have any clue how quaternions work. Would this double the processor overhead for each joint though?

The IK plugin looks nice, but it’s not what I’m looking for. This game requires Surgeon Simulator -style fumbling and stumbling to some extent. :wink:

I haven’t played Surgeon Simulator so I’ll have to take your word for it. I don’t really understand quaternions either, so maybe someone else will be able to step in with some other ideas.

I need some proper tutorials for scripting joints at all. Here’s my attempt at changing the target position of a hinge joint, but it gives the error “Cannot modify the return value of ‘UnityEngine.HingeJoint.spring’ because it is not a variable”:

//creating variables for the joint
public GameObject LeftShoulder;
public HingeJoint leftShoulderJoint;

//setting the joint to the variable
LeftShoulder = transform.Find ("LeftShoulder").gameObject;
leftShoulderJoint = LeftShoulder.GetComponent<HingeJoint> ();

//Attempt to change the targetposition of the joint, does not work.
leftShoulderJoint.spring.targetPosition++;

I’d also need to know how to work with several joints in a same part, as just GetComponent works when there’s only one hinge to control.

First let me preface all of this by saying that I’m relatively new to Unity myself, so take all of this with a grain of salt. I’m also liable to end up in over my head real quick so hopefully someone with a bit more experience can comment on this as well.

First, In the inspector (in the editor), do you have your LeftShoulder game object selected in the parameters for the script? If so, you don’t need to use “transform.Find”. If you’re instantiating objects and so forth, then maybe you will… but that’s a different discussion.

Found this:

and this

Looks like you have to go down another level and declare a variable for the spring component. I didn’t bother with a variable for the hingeJoint but you could do that as well. This code shouldn’t matter what game object it’s attached to as long as you set the desired joint in the inspector.

so this code should work (tested):

using UnityEngine;
using System.Collections;

public class elbowScript : MonoBehaviour {

    public GameObject LeftShoulder;
    public float SpringCoefficient = 30;
    public float Damper = 10;
    private float target;
    // Controls

    public KeyCode upKey = KeyCode.UpArrow;
    public KeyCode downKey = KeyCode.DownArrow;
    public KeyCode leftKey = KeyCode.LeftArrow;
    public KeyCode rightKey = KeyCode.RightArrow;
    private JointSpring shoulderSpring;

    // Use this for initialization
    void Start () {
        //creating variables for the joint

   
        shoulderSpring = LeftShoulder.hingeJoint.spring;
        shoulderSpring.targetPosition = 0;
        shoulderSpring.spring = SpringCoefficient;
        shoulderSpring.damper = Damper;
        LeftShoulder.hingeJoint.spring = shoulderSpring;

    }

    // Update is called once per frame
    void FixedUpdate () {

        if (Input.GetKey (upKey)) {
            //Attempt to change the targetposition of the joint, does not work.
            shoulderSpring.spring = SpringCoefficient;
            shoulderSpring.damper = Damper;
            shoulderSpring.targetPosition++;
            LeftShoulder.hingeJoint.spring = shoulderSpring;

                }
        else if (Input.GetKey (downKey)) {
            shoulderSpring.targetPosition--;
            shoulderSpring.spring = SpringCoefficient;
            shoulderSpring.damper = Damper;
            LeftShoulder.hingeJoint.spring = shoulderSpring;
        }
        else if (Input.GetKey (leftKey)) {

        }
        else if (Input.GetKey (rightKey)) {


        }


    }
}

You can change GetKey to GetKeyDown if you want it to be a continuous change. Just remember you’re setting to an angle, so the movement doesn’t stop immediately when you release the key.

Thanks again, apparently the problem is that C# cannot change a single value of a struct, and instead of using simply

hingeJoint.spring.targetPosition = 0;

it would need to use

    JointSpring spr = hingeJoint.spring;
    spr.targetPosition = 0;
    hingeJoint.spring = spr;

Is that slower to run, and should I do the movement code in javascript instead?

I doubt it will make much difference.

No Unity script is doing exactly the same operation behind the scenes ( compiler magic ) its just been abstracted to setting it directly.

Ok, now I have successfully made the leg move, and even found out how to add more joints via script through trial and error.

I made what seemed like a brisk gallop in the editor, but when I compile it into an executable it slows down to about 1/30th of the original speed. Physics and everything else works as fast as before, but the animation (adding 0.3 to target rotation each Update) is incredibly slow. What could be causing this, and how do I fix it?

Even more problems… Even though the limb movement is well-adjusted, the creature just randomly slides around the ground even when all its legs are still! Is there a friction setting or something to actually make the limbs stick to the ground when they’re planted?

Unity has physics materials , you can set the global material and then a material for each physics item. A physics material has parameters for how friction and bounce are calculated between surfaces.