Hello. I’m making a 3D isometric game. Movement and look direction were determined by WASD keys, so I only needed a standard forward running animation so far. However, recently I added an ability where the player’s look direction faces the mouse position on the screen when RMB is held, in order to aim in that direction. This requires 3 additional running animations, backwards, left strafe and right strafe.
So, I made a blend tree to include all 4 animations. It is clear as day to me how this is supposed to function, but I can’t for the life of me figure out what exactly I’m doing wrong. Here’s the relevant code for blending between the 4 animations:
private void Move()
{
if (Input.GetMouseButton(1))
{
anim.SetBool("Running", false);
anim.SetBool("Blending", true);
// Get the mouse position on the screen
Vector2 mousePos = Input.mousePosition;
// Get the screen width and height
float screenWidth = Screen.width;
float screenHeight = Screen.height;
// Determine which part the mouse is in
int part;
if (mousePos.x + mousePos.y < screenWidth && mousePos.x - mousePos.y > 0) part = 1; // Top
else if (mousePos.x + mousePos.y > screenWidth && mousePos.x - mousePos.y > 0) part = 2; // Right
else if (mousePos.x + mousePos.y > screenWidth && mousePos.x - mousePos.y < 0) part = 3; // Bottom
else part = 4; // Left
// Set the moveX and moveY parameters based on the part and the input direction
switch (part)
{
case 1: // Top
anim.SetFloat("moveX", _input.x); // Left -> Left strafe, Right -> Right strafe
anim.SetFloat("moveY", _input.z); // Forward -> Forward, Backward -> Backward
break;
case 2: // Right
anim.SetFloat("moveX", _input.x); // Left -> Backward, Right -> Forward
anim.SetFloat("moveY", _input.z); // Forward -> Left strafe, Backward -> Right strafe
break;
case 3: // Bottom
anim.SetFloat("moveX", -_input.x); // Left -> Right strafe, Right -> Left strafe
anim.SetFloat("moveY", -_input.z); // Forward -> Backward, Backward -> Forward
break;
case 4: // Left
anim.SetFloat("moveX", -_input.x); // Left -> Forward, Right -> Backward
anim.SetFloat("moveY", _input.z); // Forward -> Right strafe, Backward -> Left strafe
break;
}
}
else
{
anim.SetBool("Blending", false);
anim.SetBool("Running", true);
}
Vector3 movementDirection = new Vector3(_input.x, 0f, _input.z).normalized.ToIso();
_rb.MovePosition(transform.position + movementDirection * _speed * Time.deltaTime);
}
So, the screen is split into 4 parts, and the mouse position in the game is supposed to alter which animation plays when one of the movement keys is held(WASD). The code works when the mouse is in the top part of the screen, but anywhere else and it all turns to mush. I cannot figure out what I’m doing wrong in the switch statement so that the moveX/moveY parameters aren’t changing correctly.