How do I get diagonal 3d movement?
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.
The character has a running animation for every direction, including the diagonal directions. Here is my animator controller set up (image link because I can't have any more attatchments): http://orig10.deviantart.net/082a/f/2016/354/c/3/zanimator_by_blueneonkid-das9mji.png
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"));
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 ("TurningLeft", false);
}
}
}
Script #3 (As an image link because I forgot to save this one and can't add any more attachments):
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?
Answer by UnityCoach · Dec 19, 2016 at 08:07 PM
All you need is in the 2D Blend Tree in Animator. You simply expose X and Y parameters from the Animator Controller, which you manipulate from the code, like you do. Then, you add you diagonal animations to the Blend Tree and position them on the 2D graph.
"Expose the X and Y parameters"? Could you explain this a bit more, please? I'm a noob.
Well, I see you already set float parameters like this :
myAnimator.SetFloat ("VSpeed", Input.GetAxis ("Vertical"));
myAnimator.SetFloat ("HSpeed", Input.GetAxis ("Horizontal"));
I assume you already have those parameters "exposed" in the Animator Controller, right?
If so, all you need is to create a 2D blend tree and add all the different animations to it.
Yeah, I have those in the animator controller. Setup:
However, 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
Yes, there are no other layers on top. Buuuut, its' set up like this:
Is this set up causing my problem? (For some reason, I can't reply to your post)
Yes, I know, you can only reply to post I published to you. I can't see the image.
I made a forum topic so you can help me better: https://forum.unity3d.com/threads/how-do-i-get-diagonal-3d-movement-with-root-motion.447057/ Also, here's the image: