Week 8

robot in Maya

Make a Robot

There's lots of ways to draw a robot, but resist the urge to make a Kitty Robot, at least for now. Stick to Bi-peds. You can extrude or combine primitives or any other method. If you use curves translate them to polygons before we start rigging.
One nice trick is to only make half your robot, then mirror it - so only build the right half of your robot to start.
Make sure you build the robot so it's looking straight out the z axis.
Select all the objects in your robot and go edit mesh combine to make them one.
turn on the x-ray vision, go to face mode and select half the faces. This will be easier from the front view. The x-ray will allow you to select faces that are on the back side of the robot.
Go Mesh - Mirror - check-box. You'll need to make sure it's mirroring over the x axis.
maya leg rigging image

Rig a Robot

You can rig a robot by hand like we did with the Flag. Remember to always build your model in an isometric view. You'll need to use inverse kinematics so the knees and elbows bend the right way. When making an elbow or finger make the joint based on local transform not global. Otherwise your knee will bend at an odd angle. You'll also want to create and IK (Inverse Kinematics) joint so that the knees and elbows move correctly and also a controller for the IK joint so that the foot stays on the ground.
Here's a great tutorial that is 1. old so a lot of stuff is not where it's supposed to be and 2. the guy's got a cold so he sniffles, but it's a quick and efficient look at basic rigging.

Auto-Rigging a Robot

The other method is to use the auto-rig, but if you auto-rig then make sure you do the step by step so that you can set the bones in just the right spot.
Note: If you want to add a hand, go back to the auto rig and go back two steps. It looks like you need to rebuild the skeleton, not just de-skin it. Build the skeleton and add a hand, then skin again. Make sure the wrist is the parent of the hand. You want to choose "current settings" not GVB before you skin the model.

Animate a robot

There are two ways to animate someone walking and reasons to do both. You can have them walk in place and use code to move the character, or you can move the character and have their "root motion" move them through the game space. Let's start by having them walk in space. To animate use a walk cycle drawing as reference. You can go view - image pane in the scene view to place an image if you want. This might help for both animating and drawing your models. Tip: Use the hierarchy graph to select all the controllers for the animation. sure you're on the first frame and hit 's' to key frame all the controllers at one. Now you can auto-keyframe and it should work, make sure you are moving the model by moving the controllers. Make a walk, idle, and jump animation on the same time-line.
When you export select "bake animation" as well as animation. This is better than baking in Maya beforehand because its easier to make changes later.

Move a robot in Unity

Moving the character in unity with code and a Character controller. When you import the character it will have all the animations in one time-line. The trick is to chop them up into the animations you created in Maya. Make them all loop. Just like the waving flag you need an animation controller to govern the animation. To move the controller with code you do that directly instead of moving the transform or the rigidbody. There are two methods - the simple one doesn't include a y axis so the character cant jump.
simple character mover
void Update()
    float move = Input.GetAxis("Vertical");
    float turn = Input.GetAxis("Horizontal");
    anim.SetFloat("walkVar", move);
    anim.SetFloat("turnVar", turn);
    Vector3 fwd = transform.TransformDirection(Vector3.forward);
    float curSpeed = anim.GetFloat("walkVar") * Time.deltaTime * walkSpeed;
    controller.SimpleMove(fwd * curSpeed);
    transform.Rotate(0, turn * Time.deltaTime * rotSpeed, 0);
This more complicated way also lets you jump, so it's a lot more useful. In the example below there is a walkVar variable on the animation controller. Put the idle animation into the animation controller as the first animation and also drag in the walk animation. Right click to create a link between the two. Click on the link itself to add a condition that governs the transition. In this case you can say if walkVar > 0 it's okay to transition to walking. You can also make a transition back if the walkspeed drops below a certain point. YOu can easily create more conditions by adding more parameter but they will always be AND conditions. To make an OR condition (for example if you want the robot to animate if the walkVar is > .1 or < -.1) then you need to make a second transition.
moving character controller with code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class robotWalker : MonoBehaviour
	Animator anim;
	private float rotSpeed = 90.0f;
	public float speed = 6.0F;
	public float jumpSpeed = 8.0F;
	public float gravity = 20.0F;
	private Vector3 moveDirection = Vector3.zero;
	CharacterController controller;

	void Start ()
		anim = GetComponent <Animator> ();
		controller = GetComponent<CharacterController> ();

	void Update ()
		float move = Input.GetAxis ("Vertical");
		float turn = Input.GetAxis ("Horizontal");
		anim.SetFloat ("walkVar", move);
		if (controller.isGrounded) {
			moveDirection = new Vector3 (0, 0, Input.GetAxis ("Vertical")); 
			moveDirection = transform.TransformDirection (moveDirection);
			moveDirection *= speed;
			transform.Rotate (0, turn * Time.deltaTime * rotSpeed, 0);
			if (Input.GetButton ("Jump")) {
				 moveDirection.y = jumpSpeed; 
		moveDirection.y -= gravity * Time.deltaTime;
		controller.Move (moveDirection * Time.deltaTime);

Moving with Root Motion

Go back to Maya and instead of walking in place have your character move forward as it walks. In Unity you want to make sure your character's root node is set to the root bone of the model. Set the other setting to the way they are here. I'm not 100% sure it needs to be generic, but it works! You'll still need an animation controller. character controller

Then in the inspector you should be able to bake the poses for the rotation and Y transform. In this dialog under motion set the root motion to none. It should work! Make sure you have a character controller on your character.