# How do I get diagonal 3d movement when two buttons are pressed?

I have a model moving using root motion. Currently, it's moving in 4 directions; Forward, Backward, Right, and Left.

Now, I want to make the character move in 45 degrees. I made a chart to understand the axes. (link because I can't have anymore attatchments)

http:// orig15(.) deviantart(.)net /fa04/f/2016/354/8/3/unity_movement_chart_by_blueneonkid-das9e8a.png

(Remove the spaces and parenthesis (yeah, it's a bit Youtube 2009, but what can I do? I'm new here.)

The character has a running animation for every direction, including the diagonal directions. Here is my animator controller set up:

I started out with trying to move diagonally left, and tried out a few scripts.

Script #1 (The simplest):

public class Zcontroller : MonoBehaviour {

private Animator myAnimator;

// Use this for initialization
void Start () {
myAnimator = GetComponent<Animator>();
}

// Update is called once per frame
void Update () {
myAnimator.SetFloat ("VSpeed", Input.GetAxis ("Vertical"));
myAnimator.SetFloat ("HSpeed", Input.GetAxis ("Horizontal"));
myAnimator.SetFloat ("DiagSpeed", Input.GetAxis ("Vertical") + Input.GetAxis ("Horizontal"));
}
}


Note: Here, I tried combining the axes inputs.

Script #2:

 using UnityEngine;
using System.Collections;

public class Zcontroller : MonoBehaviour {

private Animator myAnimator;

// Use this for initialization
void Start () {
myAnimator = GetComponent<Animator>();
}

// Update is called once per frame
void Update () {
myAnimator.SetFloat ("VSpeed", Input.GetAxis ("Vertical"));
myAnimator.SetFloat ("HSpeed", Input.GetAxis ("Horizontal"));

if (Input.GetAxis ("Vertical") || Input.GetAxis ("Horizontal")) {

if ((Input.GetAxis ("Vertical") < 0f) && (Input.GetAxis ("Horizontal") > 0)) {
myAnimator.SetBool ("isDiagonalLeft", true);
}

} else {
myAnimator.SetBool ("isDiagonalLeft", false);
}
}
}


Here, I tried using a Boolean to trigger the animation

Script #3:

Results:

Script 1 did not work, and caused the character to move diagonally only when the Down Arrow key was pressed.

Script 2 also did not work, giving me the error: "Operator '||' cannot be applied to operands of type 'float' and 'float'."

Script 3 gave me some bracket errors.

What can I do to get the character to move diagonally properly?

Is there any way to combine two float values in code? If so, how?

EDIT: I tried a solution one guy on Unity Answers suggested, which was using a 2D Freeform Directional chart. Here's my setup for that (Link becaus I can't have any more attatchments):

http:// img04(.)deviantart(.)net/39b5/i/2016/354/9/b/zanimator_setup_2_by_blueneonkid-dasaome.png

(again, remove the spaces and parenthesis)

It still doesn't play the diagonal run animation whenever I hold two directional keys down.

I set the Diagonal Left animation to -1, 1 and the Diagonal Right animation to 1,1

(If anyone's wondering why I'm trying to achieve such an archaic way of movement, I'm trying to emulate the locomotion style of Megaman Legends 2, an Action RPG released in 2000 for the PS1.

Unlike modern Action RPGs where you can move 360 degrees, MML2 had 8 directions of movement. Kind of like Ocarina of Time.)

• Just a pointer, you describe wanting digital movement. You can use Input.GetAxisRaw to interpret input without smoothing, giving you digital input if you use keys, instead of analog. – Gnemlock Dec 24 '16 at 5:07

I'm not sure I understand your question, but I believe you are trying to move the character based on it's animation state. I personally think that the reverse method is a better method: Animate the character based on it's movement.

Especially for a game like MegaMan Legends, this is the system that I would use.

1) Construct a direction vector based on the user's input. Something like:

Vector3 inputDirection = new Vector3(Input.GetAxisRaw("Horizontal"), 0 , Input.GetAxisRaw("Vertical"));


We leave the y component empty because y velocity is upward motion, which is typically added by pressing a button once (e.g. jump button), not holding a button.

2) Normalize the vector.

Vector3 velocity = inputDirection.normalized;


Since a diagonal input would result in a vector of (+/-1, 0, +/-1) which has a magnitude of ~ 1.41, we normalize the vector to avoid diagonal movement being faster than cardinal movement.

3) Apply your character's speed factor.

velocity *= characterSpeed;


This allows you to control how fast the character runs.

4) Get jump inputs.

if(Input.GetButtonDown("Jump") && isGrounded) velocity += (0, jumpStrength, 0);


Here we add jump inputs. We use GetButtonDown and isGrounded to avoid double jumps, and you can tune the strength of the jump by changing jumpStrength. We don't normalize this vector because, true to MegaMan's design, you maintain your horizontal velocity when jumping.

6) Apply the movement to your character.

transform.Translate(velocity);


You can use a physics method here if you choose, but I find this much simpler. If you need to worry about collisions, you can use velocity to perform a RayCast to see if you will be overlapping an object after translating. If so, only translate as far as you can.

7) Send your velocity variables to mecanim.

myAnimator.setFloat("xVelocity", velocity.x);
myAnimator.setFloat("yVelocity", velocity.y);
myAnimator.setFloat("zVelocity", velocity.z);


You will use these variables to switch between your animations. You will of course need to create these variables in mecanim as well.

8) Set up your blend tree in mecanim.

You can use a Blend tree to blend your 8 animations while using the velocity variables to control the blend direction/amount. I personally would have used just 4 animations for each cardinal direction and blended between them for the diagonal directions, but if your animations are all unique in some way then it would of course be necessary to have all 8 of them.

This is the system I have used in all of my projects thus far, and it has always served me well. I hope you find this helpful.