Smoothing animation for mecanim

Hi,
I have a blend tree in mecanim set up with four animations, walking back, walking forward, running forward and sprinting forward. When I start moving forward or backward it smoothly transitions into the correct animation depending on if run is toggled on and/or if the sprint button is pressed. However if I am already moving forward and I toggle run or sprint it jumps to that new animation. I’m using a heavily modified version of the bot control script.

	void Update()
	{
		//Toggle run on or off
		if(Input.GetButtonDown("RunToggle"))
		{
			if(run == 1f)
			{	
				run = 0f;
			}
			else
			{
				run = 1f;
			}
		}
		
		//Hold sprint to keep it on or let go to turn it off
		if(Input.GetButton("Sprint"))
		{
			if(run == 1f)
			{
				sprint = 1f;
			}
			else
			{
				sprint = 2f;
			}
		}
		else
		{
			sprint = 0f;
		}
	}
	
	void FixedUpdate ()
	{
		//Mouse look on X axis. Looking accross the X axis means rotating on the Y axis. This is mirrored in the PlayerLook script
		rotationX += Input.GetAxis("Mouse X") * sensitivityX;
		transform.localEulerAngles = new Vector3(0, rotationX, 0);
		
		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
		//Add the values of the player's speed, if they are running and sprinting and times it by the forward value (1 or 0).
		float runSpeed = (1f + run + sprint) * v;
		float strafeSpeed = (1f + run) * h;
		
		
		
		anim.SetFloat("ForwardSpeed", runSpeed);							// set our animator's float parameter 'ForwardSpeed' equal to the vertical input axis				
		anim.SetFloat("StrafeSpeed", strafeSpeed); 						// set our animator's float parameter 'StrafeSpeed' equal to the horizontal input axis		
		anim.speed = animSpeed;								// set the speed of our animator to the public variable 'animSpeed'
		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		
		
	}

As you can see I set the run and sprint to a 1 or 0 (sprint also has 2 if run is off) and then add them together at the bottom with with 1 (that will later be replaced with a speed variable for character customisation) then mulitplied by the state of the horizontal axis. What I think is hapening is that the because v is an axis it is already being lerped. But I don’t know where that is. I also tried making a lerp based on delta time inside update for each of the transitions (toggling sprint or run) but it didn’t do anything (perhaps lerp can only be used in late update?)

Any help you guys can offer would be greately appreciated!

Kind regards,

I may not be understanding your problem fully, but I just was dealing with what sounds like the same thing. My character was toggling between jog and run modes, using shift. By clamping the speed value, it was resulting in a jarring switch between the two, because there was no Lerping between the two values. What I did not realize was that SetFloat has a built in dampening (a lovely surprise).

You may want to try setting the dampTime value:

anim.SetFloat("ForwardSpeed", runSpeed, 0.2f, Time.deltaTime);  
anim.SetFloat("StrafeSpeed", strafeSpeed, 0.2f, Time.deltaTime);

I hope that helps!

2 Likes

Ah, that is brilliant! Thanks so much mate I didn’t know about the dampening, I will research that further so I fully understand it. You are a champion!

Scoboo,

How are you blending the Horizontal strafing with your Vertical walking?

There are a number of threads with people trying to find a solution to this problem since a Blend Tree only accepts one paramater.

http://forum.unity3d.com/threads/169525-Mecanim-Dual-Axis-Movement

http://forum.unity3d.com/threads/160800-Mecanim-Blend-Tree-transition-animations-are-snapping-with-Angry-Bots-controls-(WASD)

So far I have not been able to find a solution using Mecanim to replicate the movement used in the Bootcamp Demo. Third Person walk forward/back with strafing left/right. At least nothing that comes even remotely close to the smoothness you see in the Bootcamp demo.

Allan