Search Unity

How do I get diagonal 3d movement with Root Motion?

Discussion in 'Animation' started by Blueneonkid, Dec 20, 2016.

  1. Blueneonkid

    Blueneonkid

    Joined:
    May 21, 2015
    Posts:
    2
    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):

    1. public class Zcontroller:MonoBehaviour{
    2. privateAnimator myAnimator;
    3. // Use this for initialization
    4. voidStart(){
    5. myAnimator =GetComponent<Animator>();
    6. }
    7. // Update is called once per frame
    8. voidUpdate(){
    9. myAnimator.SetFloat ("VSpeed",Input.GetAxis("Vertical"));
    10. myAnimator.SetFloat ("HSpeed",Input.GetAxis("Horizontal"));
    11. myAnimator.SetFloat ("DiagSpeed",Input.GetAxis("Vertical")+Input.GetAxis("Horizontal"));
    Note: Here, I tried combining the axes inputs.
    Script #2:

    1. using UnityEngine;
    2. using System.Collections;
    3. public class Zcontroller:MonoBehaviour{
    4. privateAnimator myAnimator;
    5. // Use this for initialization
    6. voidStart(){
    7. myAnimator =GetComponent<Animator>();
    8. }
    9. // Update is called once per frame
    10. voidUpdate(){
    11. myAnimator.SetFloat("VSpeed",Input.GetAxis("Vertical"));
    12. myAnimator.SetFloat("HSpeed",Input.GetAxis("Horizontal"));
    13. if(Input.GetAxis("Vertical")||Input.GetAxis("Horizontal")){
    14. if((Input.GetAxis("Vertical")<0f)&&(Input.GetAxis("Horizontal")>0)){
    15. myAnimator.SetBool("isDiagonalLeft",true);
    16. }
    17. }else{
    18. myAnimator.SetBool("isDiagonalLeft",false);
    19. }
    20. }
    21. }
    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
     
    Last edited: Dec 20, 2016
  2. Pinkuboxu

    Pinkuboxu

    Joined:
    Mar 20, 2014
    Posts:
    45
    One of the biggest problems the Mechanim method of MOB and PlayerOB stuff has caused for me in Unity really speaks to this question.

    Just to get the ball rolling, why do you need a state/animation for all 8 directions of movement? Typically, I find I need one animation for forward locomotion (regardless of x,z direction), and for polish, one for turning left and one for right (which maybe mirrored, I think...). I'm assuming you have a top down-ish or third person perspective of some type. I'd try to simplify this to a single forward movement animation and one (or two if you can't mirror) for if you have to have a left/right turning animation. Maybe describe what kind of locomotion style you are going for. Smash TV or Ocarina of Time... Something else(?). I'm grasping straws to be honest but it kinda feels like you have too many states that you don't need, unless I'm missing the reason why you have them because I don't know what game you are making. :)
     
  3. Ramaraunt

    Ramaraunt

    Joined:
    Dec 17, 2016
    Posts:
    77
    heres how I flip in 2.5d

    Code (CSharp):
    1.  
    2.     public float rotationSpeed;
    3.    
    4.     private bool facingRight = true;
    5.     private bool rotatingLeft = false;
    6.     private bool rotatingRight = false;
    7.  
    8.  
    9.  
    10. void FixedUpdate()
    11.     {
    12.  
    13.      ...
    14.  
    15.         if (move > 0 && !facingRight)
    16.         {
    17.             flip();
    18.         }
    19.         else if (move < 0 && facingRight)
    20.         {
    21.             flip();
    22.         }
    23.  
    24.         if (rotatingLeft)
    25.         {
    26.             if (transform.localEulerAngles.y < 270)
    27.             {
    28.  
    29.                 transform.Rotate(0,rotationSpeed,0);
    30.             }
    31.             else
    32.             {
    33.                 rotatingLeft = false;
    34.             }
    35.         }
    36.         else if (rotatingRight)
    37.         {
    38.             if (transform.localEulerAngles.y > 90)
    39.             {
    40.                 transform.Rotate(0, -rotationSpeed, 0);
    41.             }
    42.             else
    43.             {
    44.                 rotatingRight = false;
    45.             }
    46.  
    47.         }
    48.  
    49.  
    50.     }
    51.  
    52.     void flip()
    53.     {
    54.  
    55.         if (!rotatingLeft && !rotatingRight)
    56.         {
    57.             if (facingRight)
    58.             {
    59.                 rotatingLeft = true;
    60.             }
    61.             else
    62.             {
    63.                 rotatingRight = true;
    64.             }
    65.  
    66.  
    67.             facingRight = !facingRight;
    68.         }
    69.  
    You can use just give credit.

    Basically, it gradually spins the character by rotation-speed when move changes around 0.

    No animation needed :D
     
  4. Blueneonkid

    Blueneonkid

    Joined:
    May 21, 2015
    Posts:
    2
    I'm trying to imitate the locomotion style of Megaman Legends 2, a 3rd person Action RPG PS1 game where there are only 8 directions to move, unlike modern games, where there is full 360 movement allowed.
    Kinda like Ocarina.