Search Unity

[solved] Issue With Animation To The Right Showing - 2d Top Down

Discussion in 'Scripting' started by Huwey, Mar 21, 2019.

  1. Huwey

    Huwey

    Joined:
    Mar 17, 2019
    Posts:
    29
    Ok, I want to start by saying that I'm extremely new to all of this. I have pieced things together that I have found online so the way things are setup might not be the best way to do it. However I am having an issue with my animation for walking to the right (MoveRight) doesn't work. This is for a top down 2d game. Here is what my animator looks like. I hope it's done correctly please be kind if it's not :(.


    Also here is the coding that I pieced together to get it working.
    It does properly change from the idle animation and back on all but going right and at that point it continues showing the idle animation.
    I have went as far as to change the keys associated to the animation changes and deleting and making the animation again.
    I would be so confused as to why it isn't working if it wasn't for the fact that I setup the left and then copied and pasted the rest changing the key and animation associated to it.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerAnimation : MonoBehaviour
    6. {
    7.  
    8.     public Animator anim;
    9.     public float maxSpeed = 7;
    10.  
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.         anim = GetComponent<Animator>();
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         if (Input.GetKeyDown(KeyCode.LeftArrow))
    21.         {
    22.             anim.SetBool("MoveLeft", true);
    23.             anim.SetBool("MoveRight", false);
    24.             anim.SetBool("MoveUp", false);
    25.             anim.SetBool("MoveDown", false);
    26.             anim.SetBool("Idle", false);
    27.         }
    28.         if(Input.GetKeyUp(KeyCode.LeftArrow))
    29.         {
    30.             anim.SetBool("MoveLeft", false);
    31.             anim.SetBool("Idle", true);
    32.         }
    33.         if (Input.GetKeyDown(KeyCode.RightArrow))
    34.         {
    35.             anim.SetBool("MoveRight", true);
    36.             anim.SetBool("MoveUp", false);
    37.             anim.SetBool("MoveDown", false);
    38.             anim.SetBool("MoveLeft", false);
    39.             anim.SetBool("Idle", false);
    40.         }
    41.         if (Input.GetKeyUp(KeyCode.RightArrow))
    42.         {
    43.             anim.SetBool("MoveRight", false);
    44.             anim.SetBool("Idle", true);
    45.         }
    46.         if (Input.GetKeyDown(KeyCode.UpArrow))
    47.         {
    48.             anim.SetBool("MoveUp", true);
    49.             anim.SetBool("MoveRight", false);
    50.             anim.SetBool("MoveDown", false);
    51.             anim.SetBool("MoveLeft", false);
    52.             anim.SetBool("Idle", false);
    53.         }
    54.         if (Input.GetKeyUp(KeyCode.UpArrow))
    55.         {
    56.             anim.SetBool("MoveUp", false);
    57.             anim.SetBool("Idle", true);
    58.         }
    59.         if (Input.GetKeyDown(KeyCode.DownArrow))
    60.         {
    61.             anim.SetBool("MoveDown", true);
    62.             anim.SetBool("MoveRight", false);
    63.             anim.SetBool("MoveUp", false);
    64.             anim.SetBool("MoveLeft", false);
    65.             anim.SetBool("Idle", false);
    66.         }
    67.         if (Input.GetKeyUp(KeyCode.DownArrow))
    68.         {
    69.             anim.SetBool("MoveDown", false);
    70.             anim.SetBool("Idle", true);
    71.         }
    72.     }
    73.  
    74.     void FixedUpdate()
    75.     {
    76.  
    77.         if (Input.GetKey(KeyCode.UpArrow))
    78.         {
    79.             transform.Translate(Vector3.up * Time.deltaTime, Camera.main.transform);
    80.         }
    81.         else if (Input.GetKey(KeyCode.DownArrow))
    82.         {
    83.             transform.Translate(Vector3.down * Time.deltaTime, Camera.main.transform);
    84.         }
    85.         else if (Input.GetKey(KeyCode.RightArrow))
    86.         {
    87.             transform.Translate(Vector3.right * Time.deltaTime, Camera.main.transform);
    88.         }
    89.         else if (Input.GetKey(KeyCode.LeftArrow))
    90.         {
    91.             transform.Translate(Vector3.left * Time.deltaTime, Camera.main.transform);
    92.         }
    93.     }
    94. }



    EDIT: So I noticed this when opening the file I got the following errors in the Console

    Controller 'Player': Transition '' in state 'MoveDown' uses parameter 'MoveRight' which is not compatible with condition type.

    It shows this for all states. up, down, left, and idle

    I'm not sure what I should be looking at to fix this. Any suggestions?
     
    Last edited: Mar 23, 2019
  2. Huwey

    Huwey

    Joined:
    Mar 17, 2019
    Posts:
    29
    Anyone at all?
     
  3. Cyber-Dog

    Cyber-Dog

    Joined:
    Sep 12, 2018
    Posts:
    352
    Hey man,

    First thing I would do. Split your view in two. Animator on one side, game view on the second.
    Press play and click your animating object from the hierarchy to show the animator state.

    Now.. When you move right do you se the MoveRight bool trigger in the animator window?
    If not then your code does not trigger the animator properly.

    If you do see the animator bool trigger, but the animation stays on something else. Then it Must your animation transitions that's the issue. Check every arrow that leads to your MoveRight anim and make sure the condition is your move right bool.

    You may have simply just left the condition out, or have the wrong condition.
     
  4. Huwey

    Huwey

    Joined:
    Mar 17, 2019
    Posts:
    29
    Well in an attempt to possibly fix the issue I remade the setup of the states. Here is a screenshot to hopefully show what is happening. As far as I can tell everything is setup properly.
     

    Attached Files:

  5. Cyber-Dog

    Cyber-Dog

    Joined:
    Sep 12, 2018
    Posts:
    352
    That screen shot shows, bool is true, the blue line to the move right anim indicates it is about to start playing the move right anim. So what's the problem, is it still not playing the animation? Maybe your move right anim is actually the sprites for a different animation.
     
  6. Cyber-Dog

    Cyber-Dog

    Joined:
    Sep 12, 2018
    Posts:
    352
    Btw, I just noticed that you have everything revolving around the Idle animation. So if your going left, and you want to go another direction you first have to pass through idle, that's not good :(

    I recommend you set your Animator up in a kind of start formation, with "Any State" in the middle. So from any state you go to Idle,Left,Right,Up,Down,Etc..

    Then just have one condition from any state that checks if every move direction bool is false, then go to idle.
     
  7. Huwey

    Huwey

    Joined:
    Mar 17, 2019
    Posts:
    29
    It is showing blue because that is what I have selected so you can see how it is set up. I'll try your suggestion with the any state.
     
  8. Huwey

    Huwey

    Joined:
    Mar 17, 2019
    Posts:
    29
    Just wanted to give an update that it started working when I implemented the any state.