Mecanim: blending with multiple layers

Hi there,

perhaps anybody can help me with this:

I’m trying to setup a basic controller for my animated characters. But I can’t figure out how blending with multiple layers works correctly.

The Setup: my character should use several weapon types, so he has several animation sets (e.g. one for pistols and one for rifles). These animations should be blended over the walk, run, jump animations. Walk, run, etc. are on the base layer.
Layer 2 has a body mask for the torso and the arms. On that layer are animations like aim and fire, because for that the upper torso has to be turned right, so that the character aims to the crosshair (3rd person).
Layer 3 has a body mask only for the arms. All animations that only affect the arms and not the torso are on that layer (holding a 2 handed weapon or reload).

My character should do the following:

  1. If he has a rifle, he should hold it in idle, walk, run, etc. That means base layer + layer3.
  2. If I now press and hold the RMB he should go into the aim position = base layer + layer2.
  3. If now the RMB is released he should return to no.1 = base layer + layer3

The problem:
No.1. and 2. are working as intended, but when the RMB is released the torso stays in the turned right position, so the torso still aims to the crosshair. The arms go into the holding position again.
It seems that the upper torso didn’t get the weight of the base layer again (the weight of the torso bones from the idle animation) – and I don’t know why.
Take a look at the pic below or my pretty crappy code. One thing to say: I’m not a programmer, only a 3d artist, therefore I would appreciate if you can post some code if you know where I failed.

I think the following lines are important for the layer blending (but I can be wrong and there is something else I missed):
27 – 29 references to the states of the animator
67 – 71 Setting the layer weight according to the layer count
85 – 91 Setting layer state variables to the current state of the layers.
289 – 319 my code that should control the behavior.

Thx for your help.

using System.Collections;

// Require these components when using this script
[RequireComponent(typeof (Animator))]
[RequireComponent(typeof (CharacterController))]
[RequireComponent(typeof (Rigidbody))]
public class CharacterControl : MonoBehaviour
{
	
	
	
	[System.NonSerialized]					
	public float lookWeight;					// the amount to transition when using head look
	
	[System.NonSerialized]
	public Transform enemy;						// a transform to Lerp the camera to during head look
	
	public float animSpeed = 1.5f;				// a public setting for overall animator animation speed
	public float MoveSpeed = 1.5f;
	public float lookSmoother = 3f;				// a smoothing setting for camera motion
	public bool useCurves;						// a setting for teaching purposes to show use of curves
	public bool hasPistol;
	public bool hasRifle;

	private Animator anim;							// a reference to the animator on the character
	private AnimatorStateInfo currentBaseState;			// a reference to the current state of the animator, used for base layer
	private AnimatorStateInfo layer2CurrentState;	// a reference to the current state of the animator, used for layer 2
	private AnimatorStateInfo layer3CurrentState;
	private CharacterController col;					// a reference to the capsule collider of the character
		
	public GameObject muzzleFlash;
	public AudioClip shoot;
	
	static int idleState = Animator.StringToHash("Base Layer.Idle");	
	static int walkCycleState = Animator.StringToHash("Base Layer.WalkCycle");			// these integers are references to our animator's states
	static int jumpState = Animator.StringToHash("Base Layer.Jump");				// and are used to check state for various actions to occur
//	static int jumpDownState = Animator.StringToHash("Base Layer.JumpDown");		// within our FixedUpdate() function below
//	static int fallState = Animator.StringToHash("Base Layer.Fall");	
	static int jumpIdleState = Animator.StringToHash("Base Layer.JumpIdle");
	static int fallDownState = Animator.StringToHash("Base Layer.FallDown");
	static int runCycleState = Animator.StringToHash("Base Layer.RunCycle");
	static int jumpRunState = Animator.StringToHash("Base Layer.JumpRun");
	static int rollState = Animator.StringToHash("Base Layer.Roll");
	static int pistolAimState = Animator.StringToHash("Layer2.PistolAim");
	static int pistolFireState = Animator.StringToHash("Layer2.PistolFire");
	static int rifleHoldState = Animator.StringToHash("Layer3.RifleHold");
//	static int rifleHoldWalkState = Animator.StringToHash("Layer2.RifleHoldWalk");
	static int rifleAimState = Animator.StringToHash("Layer2.RifleAim");
	static int rifleFireState = Animator.StringToHash("Layer2.RifleFire");
//	static int rifleReloadState = Animator.StringToHash("Layer2.RifleReload");
//	static int pistolReloadState = Animator.StringToHash("Layer3.PistolReload");
	
	
	void Awake ()
	{
			muzzleFlash.SetActive(false);
	}
	
	void Start ()
	{
		// initialising reference variables
		anim = GetComponent<Animator>();					  
		col = GetComponent<CharacterController>();				
		//enemy = GameObject.Find("Enemy").transform;	
						
		if(anim.layerCount ==2)
			anim.SetLayerWeight(1, 1);
		
		if(anim.layerCount ==3)
			anim.SetLayerWeight(2, 1);
	
	}
	

	
	void FixedUpdate ()
	{
		float h = Input.GetAxis("Horizontal");				// setup h variable as our horizontal input axis
		float v = Input.GetAxis("Vertical");				// setup v variables as our vertical input axis
		anim.SetFloat("Speed", v);							// set our animator's float parameter 'Speed' equal to the vertical input axis				
		anim.SetFloat("Direction", h); 						// set our animator's float parameter 'Direction' equal to the horizontal input axis		
		anim.speed = animSpeed;								// set the speed of our animator to the public variable 'animSpeed'
		anim.SetLookAtWeight(lookWeight);					// set the Look At Weight - amount to use look at IK vs using the head's animation
		currentBaseState = anim.GetCurrentAnimatorStateInfo(0);	// set our currentState variable to the current state of the Base Layer (0) of animation
		
		if(anim.layerCount ==2)		
			layer2CurrentState = anim.GetCurrentAnimatorStateInfo(1);	// set our layer2CurrentState variable to the current state of the second Layer (1) of animation
		
		if(anim.layerCount ==3)		
			layer3CurrentState = anim.GetCurrentAnimatorStateInfo(2);	// set our layer3CurrentState variable to the current state of the third Layer (2) of animation
		
//		// LOOK AT ENEMY
//		
//		// if we hold Alt..
//		if(Input.GetButton("Fire2"))
//		{
//			// ...set a position to look at with the head, and use Lerp to smooth the look weight from animation to IK (see line 54)
//			anim.SetLookAtPosition(enemy.position);
//			lookWeight = Mathf.Lerp(lookWeight,1f,Time.deltaTime*lookSmoother);
//		}
//		// else, return to using animation for the head by lerping back to 0 for look at weight
//		else
//		{
//			lookWeight = Mathf.Lerp(lookWeight,0f,Time.deltaTime*lookSmoother);
//		}

		// TOGGLE BETWEEN WALK AND RUN
		
		if (currentBaseState.nameHash == idleState  (Input.GetKeyDown(KeyCode.RightShift)  Input.GetKeyDown(KeyCode.UpArrow)))
		
		{
			anim.SetBool("Run", true);
		}
		
		
		if (currentBaseState.nameHash == walkCycleState  Input.GetKeyDown(KeyCode.RightShift))
		
		{
			anim.SetBool("Run", true);
		}
			

		else if (currentBaseState.nameHash == runCycleState  (Input.GetKeyUp(KeyCode.RightShift) || Input.GetKeyUp(KeyCode.UpArrow)))
			{
				anim.SetBool("Run", false);	
			}
			

		
		// STANDARD JUMPING
		
		// if we are currently in a state called Locomotion (see line 25), then allow Jump input (Space) to set the 
		//Jump bool parameter in the Animator to true
		if (currentBaseState.nameHash == walkCycleState)
		{
			if(Input.GetButtonDown("Jump"))
			{
				anim.SetBool("Jump", true);
			}
		}
		
		if (currentBaseState.nameHash == idleState)
		{
			if(Input.GetButtonDown("Jump"))
			{
				anim.SetBool("JumpIdle", true);
			}
		}	
		if (currentBaseState.nameHash == runCycleState)
		{
			if(Input.GetButtonDown("Jump"))
			{
			anim.SetBool("JumpRun", true);
			}
		}
		
		// if we are in the jumping state... 
		else if(currentBaseState.nameHash == jumpState || currentBaseState.nameHash == jumpIdleState|| currentBaseState.nameHash == jumpRunState)
		{
			//  ..and not still in transition..
			if(!anim.IsInTransition(0))
			{
				if(useCurves)
					// ..set the collider height to a float curve in the clip called ColliderHeight
					col.height = anim.GetFloat("ColliderHeight");
				
				// reset the Jump bool so we can jump again, and so that the state does not loop 
				anim.SetBool("Jump", false);
				anim.SetBool("JumpIdle", false);
				anim.SetBool("JumpRun", false);
			}
			
			// Raycast down from the center of the character.. 
			Ray ray = new Ray(transform.position + Vector3.up, -Vector3.up);
			RaycastHit hitInfo = new RaycastHit();
			
			if (Physics.Raycast(ray, out hitInfo))
			{
				// ..if distance to the ground is more than 2.75, use Match Target
				if (hitInfo.distance > 2.75f)
				{
					 // MatchTarget allows us to take over animation and smoothly transition our character towards a location - the hit point from the ray.
					 // Here we're telling the Root of the character to only be influenced on the Y axis (MatchTargetWeightMask) and only occur between 0.35 and 0.5
					 // of the timeline of our animation clip
					anim.MatchTarget(hitInfo.point, Quaternion.identity, AvatarTarget.Root, new MatchTargetWeightMask(new Vector3(0, 1, 0), 0), 0.35f, 0.5f);
				}
								
			}
		}


			
					
		// JUMP DOWN AND ROLL 
		
		// if we are jumping down, set our Collider's Y position to the float curve from the animation clip - 
		// this is a slight lowering so that the collider hits the floor as the character extends his legs
		else if (currentBaseState.nameHash == fallDownState)
		{
			col.center = new Vector3(0, anim.GetFloat("ColliderY"), 0);
		}
		
		// if we are falling, set our Grounded boolean to true when our character's root 
		// position is less that 0.6, this allows us to transition from fall into roll and run
		// we then set the Collider's Height equal to the float curve from the animation clip
		else if (currentBaseState.nameHash == fallDownState)
		{
			col.height = anim.GetFloat("ColliderHeight");
		}
		
		// if we are in the roll state and not in transition, set Collider Height to the float curve from the animation clip 
		// this ensures we are in a short spherical capsule height during the roll, so we can smash through the lower
		// boxes, and then extends the collider as we come out of the roll
		// we also moderate the Y position of the collider using another of these curves on line 128
		else if (currentBaseState.nameHash == rollState)
		{
			if(!anim.IsInTransition(0))
			{
				if(useCurves)
					col.height = anim.GetFloat("ColliderHeight");

				col.center = new Vector3(0, anim.GetFloat("ColliderY"), 0);
				
		}
		}
		
		
				
		// Pistol
		
		if (hasPistol == true)
		{			
//			if (currentBaseState.nameHash == idleState  ||currentBaseState.nameHash == walkCycleState || currentBaseState.nameHash == runCycleState)
//				{
//				anim.SetBool("PistolHold", true);
//				}
		
		
			if (currentBaseState.nameHash == idleState || currentBaseState.nameHash == walkCycleState)
			{
				if(Input.GetKey(KeyCode.Mouse1))
				{
					anim.SetBool("PistolAim", true);
				}
			}
					
			if(layer2CurrentState.nameHash == pistolAimState)
			{
				if(Input.GetKey(KeyCode.Mouse1) == false || (currentBaseState.nameHash == runCycleState))
				{
					anim.SetBool("PistolAim", false);
				}
			}
			
			if(layer2CurrentState.nameHash == pistolAimState)
			{
				if(Input.GetKeyDown(KeyCode.Mouse0))
				{
					anim.SetBool("PistolFire", true);
					muzzleFlash.SetActive(true);
					audio.PlayOneShot(shoot);
				}
				
			}
			
			if(layer2CurrentState.nameHash == pistolFireState)
			{
				if(Input.GetKeyUp(KeyCode.Mouse0) == false)
				{
					anim.SetBool("PistolFire", false);
					muzzleFlash.SetActive(false);
				}
				
			}
			
			if(layer2CurrentState.nameHash == pistolFireState)
			{
				if(Input.GetKeyUp(KeyCode.Mouse1) == false)
				{
					anim.SetBool("PistolFire", false);
					anim.SetBool("PistolAim", false);
				}
			}
		}
		
		//RIFLE
		
		if (hasRifle == true)
		{
				
			
				if (currentBaseState.nameHash == idleState)
				{
					anim.SetBool("RifleHold", true);
					
				}
			
				if (currentBaseState.nameHash == idleState || currentBaseState.nameHash == walkCycleState)
				{
					if(Input.GetKey(KeyCode.Mouse1))
					{
						anim.SetBool("RifleAim", true);
						anim.SetBool("RifleHold", false);
						anim.SetLayerWeight(1, 1);
					}
				}
		
				if(layer2CurrentState.nameHash == rifleAimState)
				{
					if(Input.GetKey(KeyCode.Mouse1) == false || (currentBaseState.nameHash == runCycleState))
					{
						anim.SetBool("RifleAim", false);
						anim.SetLayerWeight(1, 0);
						anim.SetLayerWeight(0, 1);			
//						
//						anim.SetLayerWeight(2, 1);
//						anim.SetLayerWeight(0, 1);
//						
					}
				}
			
				if(layer2CurrentState.nameHash == rifleAimState)
				{
					if(Input.GetKeyDown(KeyCode.Mouse0))
					{
						anim.SetBool("RifleFire", true);
						muzzleFlash.SetActive(true);
						audio.PlayOneShot(shoot);
					}
					
				}
			
				if(layer2CurrentState.nameHash == rifleFireState)
				{
					if(Input.GetKeyUp(KeyCode.Mouse0) == false)
					{
						anim.SetBool("RifleFire", false);
						muzzleFlash.SetActive(false);
					}
					
				}
				
//				if (layer2CurrentState.nameHash == rifleHoldState || layer2CurrentState.nameHash == rifleAimState)
//				{
//					if(Input.GetKey(KeyCode.R))
//					{
//						anim.SetBool("RifleReload", true);
//					}
//				}
			
			
		}
//		if (currentBaseState.nameHash == idleState || currentBaseState.nameHash == walkCycleState || layer2CurrentState.nameHash == pistolAimState)
//		{
//			if(Input.GetKeyDown(KeyCode.R))
//			{
//				anim.SetBool("PistolReload", true);
//			}
//		}
		
	}
	
//	void OnAnimatorMove()
//	{
//		// Set up for a rigidbody - set the RB position equal to the animator deltaPosition and increase by MoveSpeed
//		rigidbody.position += anim.deltaPosition * MoveSpeed;
//		transform.rotation *= anim.deltaRotation; 
//	}
}

These are my Blend Trees.

Ok, I figured something out.

In the Blend Trees on layer 3, the rifle hold anim blends back to “new State”. but on layer 2 the rifle aim doesnt`t blend to “new State”. But why?

Seems that is a part of the problem.

Hmm,

I rebuilt the blend tree and now the blending is displayed correctly but the problem hasn`t gone.

Bumping for OP as I’ll be getting into layer blending shortly and either I’ll figure it out (and respond) or hopefully someone else who experienced this will before then.

-Steve

Just wondering how far you’ve gotten here?

It’s rather frustrating that almost no Mecanim questions ever get answered by Unity Support when it appears very few people in the community can. When you consider how poor the documentation is ‘overall’, it’s even more remarkable that a premiere animation tool is barely touched upon.

Cheers

It seems some features like additive layers are not really used by anyone, at least I didn’t manage to find anybody that managed to use them successfully:

http://forum.unity3d.com/threads/180035-Mecanim-root-motion-and-additive-layer-bug

Otherwise for his problem it’s hard to debug like that, you really need to pause the game, inspect the animator state, and print out all the relevant variables.

Well so be it…I have no real choice but to explore the in’s and out’s of layers lest I want to duplicate each of my anims for each new weapon/item.

Perhaps this week I’ll make it my ‘aim’ to make a full go if it and see what’s what.

As for your other thread, yea I’d be surprised if doing additive root motion wasn’t problematic. On one hand you’re adding the root motion together, and the result should be the ‘root motion’ applied the the CharacterController, but perhaps it isn’t that simple. I don’t have an answer for you either but I can put it on the list as I’m layering.

Not having anything to test this moment, I’d ask he try out transition into and out of Any State to see if he can ensure that all sequences link back up to base in every and any instance.

Thanks Staross

Oh and while I have you, any thoughts on this:

http://forum.unity3d.com/threads/182080-Biped-Rotations-Incorrect-FBX-Max-or-Unity-issue

I have a simple errant bone rotation, and I have no clue why certain anims do this, and others don’t, and why the error can also occur ‘during’ an animation for a single frame or two, yet the rest of the sequence is fine.

I suspect it’s that my character is not in a “legal” T-Pose, but that would be rather inflexible as that would require substantial reworking of my rig and animations.

Thanks one more time! :smile:

I can’t really help you there, I’m using blender and I never had such problem. I had some similar problems with extreme root bone rotation (flip animation) but it was fixed by using the bake rotation option in the animation import. Maybe it’s an issue of interpolation between keyframes ?

@Staross

Apologies for rev. old thread but I thought I’d add this in. If you experience a horrible rotation glitch through your animation it is likely that you’ve rotated something the whole way round and copied and pasted the start frame to the end. If you do this you can solve the problem by simply rotating the thing around 360 degrees the other way before keyframing it, or simply adding (or removing) a minus in the relevant rotation if it all goes around an origin.