# Controlling a four legged creature

Howdy!

We are currently making a game where you control a four legged creature. The creature runs obstacle courses (on time) and needs to be able to knock the obstacles down resulting in a time penalty.

Right now the creature is controlled using a character controller placed between the head and the front legs. When the controller hits a rigid body obstacle a script pushes it away with a force depending on the creatures speed. This works somewhat OK.

Problems:

1. The character controller can not define an “hit area” accurate enough since it has the shape of a capsule.
2. You can only have 1 character controller which means the back part of the creature can not collide with obstacles.
3. When jumping the front legs will “land” before the back legs and as the capsule is placed in the front the creature thinks he has landed before the whole body has landed which means you can jump again as soon as the front legs are grounded which would actually not be possible.

Have anyone design a more accurate colliding four legger?

Possible solutions:
A) Create simple boxes (either rigid bodies or box colliders with “pusher” scripts, don’t know what works best) and parent them to appropriate parts of the body and have them handle collision with obstacles. Character controller will only be used to handle movement and making sure the creature doesn’t fall through the terrain.
B) Create a simple collision mesh (in Maya or similar) that is animated as the creature is). This should make for a pretty accurate collisions with obstacles. Character controller will be used in same way as in (A).
C) Scrap the character controller and use physics to drive the four legger. This seems like it could get very complex and is a route that I would not like to go down if it can be avoided.

Box colliders, character controllers, rigid bodies… I’m so confused. Help would be much appreciated.

Physics isn’t that hard in unity and it won’t be that complex once you start. But what do you have in mind for collisions via physics? Raycasting? I am working on a dog for my game so I know that it isn’t too hard but if your new to unity it might be, I don’t know. (Raycasting can be used as another form of collisions)

I’d do A + C. The CharacterController is mostly just useful for walkthrough demos - most people build their own physics based character controller. Theres an example on the wiki.

bloodtiger10, when you say collision I’m guessing you mean collision with the ground? And I’m guessing raycasting would work kind of like old school line collision? Two physic bodies colliding (creature and obstacle) would sort themselves out in the physics engine I presume?

AngryAnt, A and B, interesting… care to elaborate? Thanks for the link btw, creating my own character controller seems like a good idea.

Tried parenting rigid body boxes to the legs of the creature with out much effect. I am guessing that’s because the rigid body doesn’t pick up any force even though the creature is moving. That could be done via script though. Also having problems with the rigid bodies colliding with each other and the controller capsule. Any way to filter that? Back to the drawing board.

Ah sorry I meant to say A + C. Make sure that your animation has “animate physics” ticked.

Ahh, that seems to be a cool feature! Didn’t know it existed. Our artist did the importing of models. I’ll try it right away. Thanks AngryAnt!

Wow, that worked wonders! I am still running with the unity shipped character controller. For some reason it offset slightly during game play which means it sometimes starts colliding with the parented rigid bodies which makes for interesting results.

Anyone know why the controller position moves about? Can the rigid bodies be masked to only collide with certain layers?

OK, tried the script form the wiki posted above. Put that on a rigid body capsule collider that I placed in the creatures body. I then parented rigid body box colliders to the legs. Problem is the creature falls half ways through the terrain an collides with the capsule instead of standing on its feet.

viewed from the side (dots are just to make the shape stay intact, seems posting removes spaces):
.____
(____) <— capsule with controller script.
|…|
[ ]…[ ] <— rigid body box colliders.

I could make a compound collider off primitive colliders rougly in the shape of the creature and put the script on that but then the leg colliders won’t follow the creatures animations which is kind of vital when the creature jumps over obstacles.

Ideas?

Kinda, I meant have a ray casting to the ground to determine in the distance from the ground and also the distance your creatures legs are the the ground.

Also Raycasting would need your own code for objects colliding so

``````function Update () {
var hit : RaycastHit;
var fwd = transform.TransformDirection(Vector3.forward);
if (Physics.Raycast (transform.position, fwd, 1)) {
print ("There is something in front of the object!");
}
}
``````

you would need to determine what to do so if your character is a rigidbody then

``````function Update () {
var hit : RaycastHit;
var fwd = transform.TransformDirection(Vector3.forward);
if (Physics.Raycast (transform.position, fwd, 1)) {
}
}
``````

then customize it to your need that could help for making a object collision system. (My way probily isn’t the most effective so … you would need to tweak it).

Then for creatures leg’s distance to the ground could help with the falling through the terrain because I use the distance to the ground to make planes and race cars (from my game skyway racer) float and not touch the ground.

Aye! I reread your post just after lunch and realized what you meant and started working with using raycast to keep the horse at the correct distance from the ground. So far no such luck but I’ll get it working Thanks a lot for the help.

I ended up with:

.____
(____) <— (body) capsule with controller script.
|…|<–| — raycast against terrain
[ ]…v…[ ] <— (legs) rigid body box colliders.
--------- <— (ground)

``````var hit : RaycastHit;
if (Physics.Raycast(transform.position + (Vector3.up), -Vector3.up, hit, 1.0, layerMask.value))
{
transform.position.y = hit.point.y;
velocity.y = 0;
grounded = true;
}
``````

The raycast is part of the controller script and keeps creature from to falling through the ground.

This seems to work pretty well. Is this similar to what you did bloodtiger10?

I got the creature to control pretty much the way I want it to. A problem though. If there is no input after turning the body it will continue if the direction of the force but I would like it to continue in the direction the creature is facing. Kind of like turning the current force or velocity in the direction of the creature to avoid it looking like it is walking on ice. Not sure this is clear enough for anyone to understand. Howler back if code or more info is needed.

//other random info

the locomotion project can support quadpeds but I havent tried it myself.

AC