Bizarre non-responsive button/Mecanim issues

Heya UnityAnswers long time no see.

I’m trying to get my character too animate using Mecanim. The animations will work depending on the position of the player and is controlled at the moment with the WSAD keys being Up, Down, Left, Right.

I’ve got this bit of code in the update to deal button press/movement

	float h = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed; //sets left/right to arrow keys * game speed
    float v = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;	//sets up/right to arrow keys * game speed
		transform.Translate(h, v, 0); //Will cause the player too move
		transform.position = new Vector3(Mathf.Clamp(transform.position.x, -13f, 13f), Mathf.Clamp(transform.position.y, 0.5f, 16f), transform.position.z); //Prevents player going off screen

I’ve got parameters set up in Mecanim like so -

25067-p.jpg

Then I have this code too control the left and right blending

				if(Input.GetAxis("Horizontal") < 0)
	{
		if(Input.GetKey("a"))
		{
		animator.SetBool("Left", true);
		animator.SetFloat("MoveX", transform.position.x);
	
		}
		
		if(Input.GetKeyUp("a"))
		{
		animator.SetBool("Left", false);
		animator.SetFloat("MoveX",  transform.position.x, 0, Time.deltaTime);
		
		}
	}
	
	if(Input.GetAxis("Horizontal") > 0)
	{
		if(Input.GetKey("d"))
		{
		animator.SetBool("Right", true);
		animator.SetFloat("MoveX", transform.position.x);
		
		}
		
		if(Input.GetKeyUp("d"))
		{
		animator.SetBool("Right", false);
		animator.SetFloat("MoveX",  transform.position.x, 0, Time.deltaTime);
		}
	}

This will now at least change the “MoveX” Float in Mecanim, when I press the a or d button I see the value shift according to the “transform.position.x” and the Bools turn True and False correctly.

How ever when I try to do the same with Up and Down…

		if(Input.GetAxis("Vertical") < 0)
		{
			if(Input.GetKey("w"))
			{
			animator.SetBool("Up", true);
			animator.SetFloat("MoveY", transform.position.y);
			
			}
			
			if(Input.GetKeyUp("w"))
			{
			animator.SetBool("Up", false);
			animator.SetFloat("MoveY",  transform.position.y, 0, Time.deltaTime);
			}
		}
		
			
		if(Input.GetAxis("Vertical") > 0)
		{
			if(Input.GetKey("s"))
			{
			animator.SetBool("Down", true);
			animator.SetFloat("MoveY", transform.position.y);
			
			}
			
			if(Input.GetKeyUp("s"))
			{
			animator.SetBool("Down", false);
			animator.SetFloat("MoveY",  transform.position.y, 0, Time.deltaTime);
			}
		
		}

The “MoveY” float won’t change, infact if even if I just use

		if(Input.GetAxis("Vertical") < 0)
		{
			if(Input.GetKey("w"))
			{
			Debug.Log("W HELD DOWN");
			
			}
			
			if(Input.GetKeyUp("w"))
			{
			Debug.Log("W LET GO");
			}
		}

…the console won’t even log the W button being pressed and the only way I can get it to notice it has, is if I’m holding down the S button first, like it’s waiting for that button to be held down before printing “W HELD DOWN”

So that’s the first issue, why aren’t the W/S buttons working like the A/D buttons? It’s the same code but the W/S buttons won’t respond unless I’m holding down the other button first (Pressing W will only change “MoveY” if S is being held down first and vice versa)

The 2nd far less important issue is I thought that using…

	if(Input.GetKeyUp("d"))
			{
			animator.SetBool("Right", false);
			animator.SetFloat("MoveX",  transform.position.x, 0, Time.deltaTime);
			}

…would mean that once the D button has been lifted then “MoveX” will smoothly transition from the current “transform.position.x” to zero.

Any ideas why that the W/S buttons are acting so strange despite being the same as the A/D buttons? does it have something to do with the clamping?

in this code
`

if(Input.GetAxis("Vertical") < 0)
       {
         if(Input.GetKey("w"))
         {
         Debug.Log("W HELD DOWN");
 
         }
 
         if(Input.GetKeyUp("w"))
         {
         Debug.Log("W LET GO");
         }
       }

`
you are saying
if VerticalAxis is less than zero
and the key is W
then say “W HELD DOWN”

the problem is that VerticalAxis will return a positive number when W is pressed. So if evaluation will fail. If hovever you press S, then VerticalAxis will return -1 and the succeding W press will be caught and “W HELD DOWN” will be printed.

I would suggest that you read up a bit on how axis works and how you can use them to control mechanim. It seems to me that you are overcomplicating matters a bit.

You should be able to send the axis directly to the animator like this:

_animator.SetFloat(“Vertical”, VerticalAxis);

if you then set up your blendtree to choose backward or foreward movement based on the Vertical parameter beeing a positive or a negative, and going to idle if it’s 0 you should be good to go.