Search Unity

Top Down RPG sprite glitch

Discussion in 'Animation' started by Hunterhusker, Nov 19, 2017.

?

Would you like to see a video of the issue?

  1. yes

    1 vote(s)
    50.0%
  2. no

    1 vote(s)
    50.0%
  1. Hunterhusker

    Hunterhusker

    Joined:
    Nov 19, 2017
    Posts:
    3
    This is my first game so I am sorry for messy code and all of my noobishness. I learned Java and I am using this game to help me get used to C# so I know things about coding but I'm just a little lost here. If I press W&D to move my player diagonal, and then I release the last one that I pressed, then I go to that one's idle animation. Its a little complicated to explain, so I might link a video later if nobody knows what I mean. Anyway here is my animation window(It is super complicated and layed out terrible I am so sorry.) [Just Noticed left and right animations are backwards oops but I still need help here]

    And then here is my code, which is super messy as I havent dont anything with animation before in my life. Just moving robots.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class move : MonoBehaviour {
    6.     public float speed;
    7.     Animator animator;
    8.     private bool isMoving;
    9.     // Use this for initialization
    10.     void Start () {
    11.         speed = 3;
    12.         animator = GetComponent<Animator>();
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update () {
    17.         float axisX = Input.GetAxis("Horizontal"); //horizontal movement float
    18.         float axisY = Input.GetAxis("Vertical"); //vertical movement float
    19.  
    20.         animator.SetBool("isMoving", isMoving); // check if the player is moving
    21.  
    22.         isMoving = false; //set it to false by default so the ides will show
    23.         if (Input.GetKeyDown(KeyCode.W)) //up animation
    24.         {
    25.             animator.SetInteger("walkingState", 1);
    26.             isMoving = true;
    27.         }
    28.         else if (Input.GetKeyDown(KeyCode.D)) //left animation
    29.         {
    30.             animator.SetInteger("walkingState", 2);
    31.             isMoving = true;
    32.         }
    33.         else if (Input.GetKeyDown(KeyCode.S)) //down animation
    34.         {
    35.             animator.SetInteger("walkingState", 0);
    36.             isMoving = true;
    37.         }
    38.         else if (Input.GetKeyDown(KeyCode.A)) // right animation
    39.         {
    40.             animator.SetInteger("walkingState", 3);
    41.             isMoving = true;
    42.         }
    43.         else if (Input.GetKeyUp(KeyCode.S) && isMoving == false) // bool test (unsucessful)
    44.         {
    45.             animator.SetInteger("walkingState", 4);
    46.         }
    47.         else if (Input.GetKeyUp(KeyCode.W) && (!Input.GetKey(KeyCode.D) || !Input.GetKey(KeyCode.A))) // and and or test (unsuccessful)
    48.         {
    49.             animator.SetInteger("walkingState", 5);
    50.         }
    51.  
    52.         transform.Translate(new Vector3(axisX, axisY) * Time.deltaTime * speed); //player movement handeler
    53.     }
    54.  
    55. }
     
    Last edited: Nov 19, 2017
  2. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    976
    I suggest looking at blend trees, as that will reduce this mess and most likely solve ur issue in the progress. Not to mention it's the "correct" way to handle this, and you should learn it the right way from the begining, makes things a lot easier :)
     
  3. Hunterhusker

    Hunterhusker

    Joined:
    Nov 19, 2017
    Posts:
    3
    Ok, I haven't heard of Blend tree before. I'll look into it hopefully this fixes my mess. If you don't mind I'd like to ask where I shoudl go to learn. I know there is great documentation here, but I really like youtube tutorials. Do you know of any I could watch? I looked for a while and all I found what the setinteger thing that I am doing now. Thank you though I hope this works!
     
  4. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    976
    Sure, Here is one on blend tree anim:



    His series is a good starting point. I do not agree with all his methods but thats nothing u need to worry about until way later.
     
  5. Hunterhusker

    Hunterhusker

    Joined:
    Nov 19, 2017
    Posts:
    3
    Thank you, this will help alot. :)