Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Need help on topdown 2d movement Idle

Discussion in 'Animation' started by ThirdFallenKing, Sep 19, 2022.

  1. ThirdFallenKing

    ThirdFallenKing

    Joined:
    Sep 19, 2022
    Posts:
    2
    Hello all, new poster/Unity user here.

    I've been interested in making a 2d top-down rpg like game for a while now and decided to try with Unity. To learn how to make the 8 directional movement I decided to watch a several years old video tutorial on how to animate directional movement and the idle animation that plays when the movement stops.

    I've been using this video:

    Or "Topdown 2D RPG In Unity - 04 Idle & Face Correct Direction" by Hundred Fires Games on Youtube.

    (The other three videos before this in the series just cover the setup, movement, and animation, which I don't think I need help with).

    Everything went well so far, the 8 directional movement animation is fine, until I got to the idle animation. it ends up automatically using the face down idle animation (facing towards the camera) no matter which direction once the movement ended. I want the player object to face whatever direction that the movement ended in.

    Here is my code.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.  
    8.     private Rigidbody2D myRB;
    9.     private Animator myAnim;
    10.  
    11.     [SerializeField]
    12.     private float speed;
    13.  
    14.  
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.         myRB = GetComponent<Rigidbody2D>();
    19.         myAnim = GetComponent<Animator>();
    20.  
    21.  
    22.  
    23.  
    24.  
    25.     }
    26.  
    27.     // Update is called once per frame
    28.     void Update()
    29.     {
    30.         myRB.velocity = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")) * speed;
    31.  
    32.         myAnim.SetFloat("Movex", myRB.velocity.x);
    33.         myAnim.SetFloat("Movey", myRB.velocity.y);
    34.  
    35.         if (Input.GetAxisRaw("Horizontal") == 1 || Input.GetAxisRaw("Horizontal") == -1 || Input.GetAxisRaw("Vertical") == 1 || Input.GetAxisRaw("Vertical") == -1) ;
    36.         {
    37.             myAnim.SetFloat("LastMovex", Input.GetAxisRaw("Horizontal"));
    38.             myAnim.SetFloat("LastMovey", Input.GetAxisRaw("Vertical"));
    39.         }
    40.  
    41.     }
    42. }
    43.  
    I would add images, but I can't seem to upload from imgur.


    I basically used the exact same script, animation, sprites, and directions as the video. Is there some little thing I'm missing? Is the problem a difference between versions?

    Thanks.

    edit: I am using Unity 2021.3.10f1
     
    Last edited: Sep 19, 2022
  2. ThirdFallenKing

    ThirdFallenKing

    Joined:
    Sep 19, 2022
    Posts:
    2
    I'm so annoyed now that I figured it out. The problem was the damn semicolon on the end of line 35. Strange though, when I tried to run my program without it before, my player object kept on flashing between animations like crazy. Now I can move on, thank god.
     
  3. mr_noodler

    mr_noodler

    Joined:
    May 11, 2019
    Posts:
    7
    Hi ThirdFallenKing, I made a video with a better way to control player movement and animations using enums, this is clean, easy to understand, and works beautifully: