Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Problem with Idle animation

Discussion in '2D' started by BlackwaterMerk, Jan 12, 2018.

  1. BlackwaterMerk

    BlackwaterMerk

    Joined:
    Jan 11, 2018
    Posts:
    7
    Hello I started the 2d Tutorial and am having an issue. I have an idle down animation and got it to work, added my other idle animations, set up parameters for it to change from down to right but when I play the game and move to the right it won't change animations as if the parameters aren't being met. I tried manually changing the parameters and at that point it finally changed animations so it seems like the parameters in animator aren't being fed the info from when my player moves, any help would be appreciated!!!
     

    Attached Files:

    ilker105 likes this.
  2. BlackwaterMerk

    BlackwaterMerk

    Joined:
    Jan 11, 2018
    Posts:
    7
    Using Unity 2017.3
     
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Pictures are nice when something can be easily explained in words (or code) :)
    However, if you look at this page, you can see how to add code, formatted nicely, into the thread:
    https://forum.unity.com/threads/using-code-tags-properly.143875/

    Any errors in the console? Debug.Log the values in your animate movement method, do they print correctly? I assume all is well, as you didn't mention any movement issues.

    If you look at the animator while the game is running (perhaps click your character in the hierarchy, too to make sure the animator is updating), do you see any values changing?
     
    BlackwaterMerk likes this.
  4. BlackwaterMerk

    BlackwaterMerk

    Joined:
    Jan 11, 2018
    Posts:
    7
    When running the x and y parameters I set in Animator aren't moving from 0.0, gonna post a video if I can. I can also post in my scripts thank you :)
     
  5. BlackwaterMerk

    BlackwaterMerk

    Joined:
    Jan 11, 2018
    Posts:
    7
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public abstract class Character : MonoBehaviour
    {

    //playerDirection
    protected Vector2 direction;

    //player speed
    [SerializeField]
    private float speed;

    private Animator animator;

    // Use this for initialization
    void Start()
    {
    animator = gameObject.GetComponent<Animator>();


    }

    // Update is called once per frame
    protected virtual void Update()
    {
    Move();


    }

    public void Move()
    {
    transform.Translate(direction * speed * Time.deltaTime);

    AnimateMovement(direction);

    }

    public void AnimateMovement(Vector2 direction)
    {

    animator.SetFloat("x" , direction.x);
    animator.SetFloat("y" , direction.y);
    }

    }
     
  6. BlackwaterMerk

    BlackwaterMerk

    Joined:
    Jan 11, 2018
    Posts:
    7
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Player : Character
    {





    // Use this for initialization
    void Start ()
    {

    }

    // Update is called once per frame
    protected override void Update ()
    {
    GetInput();

    base.Update();

    }



    private void GetInput()
    {
    direction = Vector2.zero;

    if (Input.GetKey(KeyCode.W))
    {
    direction += Vector2.up;
    }
    if (Input.GetKey(KeyCode.A))
    {
    direction += Vector2.left;
    }
    if (Input.GetKey(KeyCode.S))
    {
    direction += Vector2.down;
    }
    if (Input.GetKey(KeyCode.D))
    {
    direction += Vector2.right;
    }
    }
    }
     
  7. BlackwaterMerk

    BlackwaterMerk

    Joined:
    Jan 11, 2018
    Posts:
    7

    Noticed my console.......
    NullReferenceException: Object reference not set to an instance of an object
    Character.AnimateMovement (Vector2 direction) (at Assets/Scripts/Character.cs:44)
    Character.Move () (at Assets/Scripts/Character.cs:37)
    Character.Update () (at Assets/Scripts/Character.cs:28)
    Player.Update () (at Assets/Scripts/Player.cs:23)
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    For me, I remembered the general sense of your code. For other people it's good. You should look at my previous post for how to do it nicely (code tags), and edit your posts.:)

    Not sure what is wrong. Provided the variables don't have spaces by mistake and are the proper case, which they appeared to be in the screenshot, and your direction is working to move... :)
     
  9. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Excellent lol so that is the animator is null? Either remove Start() from your derived class (I think) or override it and call the base.Start().
     
    ilker105 likes this.
  10. BlackwaterMerk

    BlackwaterMerk

    Joined:
    Jan 11, 2018
    Posts:
    7
    Removing Start from the derived class (Player) was all I needed to do it seems, now it's transitioning fine! Thank you!!!!!!
     
  11. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    No problem :)