Not exactly sure whether or not this counts as Animation or Scripting, but,
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:
I started out with trying to move diagonally left, and tried out a few scripts.
Script #1 (The simplest):
- public class Zcontroller:MonoBehaviour{
- privateAnimator myAnimator;
- // Use this for initialization
- voidStart(){
- myAnimator =GetComponent();
- }
- // Update is called once per frame
- voidUpdate(){
- 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{
- privateAnimator myAnimator;
- // Use this for initialization
- voidStart(){
- myAnimator =GetComponent();
- }
- // Update is called once per frame
- voidUpdate(){
- 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);
- }
- }
- }
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:
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

