Search Unity

Animation problem unity - Simple fix

Discussion in 'Animation' started by reviveearly, Apr 29, 2021.

  1. reviveearly

    reviveearly

    Joined:
    Feb 20, 2019
    Posts:
    13
    Hey forum, I posted here yesterday but my thread seems to have disappeared into thin air. My issue is with my animation if anybody could kindly help :)

    Code (csharp):
    1.  
    2. if ((Input.GetKeyDown(KeyCode.Q)))
    3.             {
    4.                 bool fire = Input.GetButtonDown("Q");
    5.                 animator.SetTrigger("walking");
    6.                 animator.SetFloat("walk", 5);
    7.             }
    8.  
    Currently nothing plays when Q is pressed.

    My animator -

    https://rapidshare.io/Ttw/Capture.PNG

    if walk is more than 0, then play animation. If walk is less than 0 then idle. My jump works with a grounded variable, if grounded is false jump, if grounded then don't jump.

    I have no idea what I've done wrong as I'm new to unity and following tutorials online. Thanks for any help!
     
  2. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    491
    Not 100% sure if this will fix it, but first of all clean up your animator. You do not need that walk 0 bool and walking trigger, and they might mess up some logic (can't tell without a full overview of the animator and where everything exactly gets used).
    Also keep in mind that Input.GetKeyDown only returns true on the frame where you begin pressing down a button, and not on all the next frames if you keep holding the button down. If you want to be able to register keyholds, you need to use Input.GetKey, Input.GetAxis, or Input.GetButton. (GetAxis is the recommended option since then you're not hard-coding the exact button press, but instead use the Input axis which are configurable in user options and such).
     
  3. reviveearly

    reviveearly

    Joined:
    Feb 20, 2019
    Posts:
    13
    Hello, thanks for the advice. I re-done it and cleaned it up a bit, but still nothing.

    Full code:
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using PresentationCore.dll;
    6.  
    7. namespace PresentationCore.dll
    8. {
    9.  
    10.     public class Howler : MonoBehaviour
    11.     {
    12.         public Rigidbody2D myRigidBody;
    13.         public float moveSpeed = 4;
    14.         public float jumpSpeed = 100;
    15.         private Animator myanim;
    16.  
    17.  
    18.         // Use this for initialization
    19.         void Start()
    20.         {
    21.  
    22.             myRigidBody = GetComponent<Rigidbody2D>();
    23.         }
    24.  
    25.         // Update is called once per frame
    26.         void Update()
    27.         {
    28.             Animator animator;
    29.             animator = GetComponent<Animator>();
    30.             if (Input.GetAxisRaw("Horizontal") > 0f)
    31.             {
    32.                 myRigidBody.velocity = new Vector3(moveSpeed, myRigidBody.velocity.y, 0f);
    33.                 myanim.SetFloat("Speed", Mathf.Abs(myRigidBody.velocity.x));
    34.             }
    35.             else if ((Input.GetAxisRaw("Horizontal") < 0f))
    36.             {
    37.                 myRigidBody.velocity = new Vector3(-moveSpeed, myRigidBody.velocity.y, 0f);
    38.             }
    39.             else
    40.             {
    41.                 myRigidBody.velocity = new Vector3(0f, myRigidBody.velocity.y, 0f);
    42.             }
    43.  
    44.             if (Input.GetKeyDown(KeyCode.Space))
    45.             {
    46.                 myRigidBody.velocity = new Vector3(myRigidBody.velocity.x, jumpSpeed);
    47.             }
    48.  
    49.            // myanim.SetFloat("speed", Mathf.Abs(myRigidBody.velocity.x));
    50.  
    51.         //    bool fire = Input.GetButtonDown("Q");
    52.          //   if ((Input.GetKey(KeyCode.Q)))
    53.          //   {
    54.          //       animator.SetFloat("speed", 1);
    55.          //   }
    56.         }
    57.     }
    58. }
    59.  
    And these are my animation, animator and character animator screenshots:
     

    Attached Files:

  4. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    491
    You're not calling the updates on the right Animator variable.
    First you declare the member variable Animator myanim, but then you never assign anything to it.
    After that you declare another Animator animator in your update function and assign the component to it, but you call the SetFloat method from myanim, not animator.

    You should simply delete the Animator animator declaration, and in start assign GetComponent<Animator> to myanim, and everything should at least do something.

    More cleanup, in other words ;)
     
  5. reviveearly

    reviveearly

    Joined:
    Feb 20, 2019
    Posts:
    13
    It works! Thank you very much, your help is appreciated. :) Excellent.
     
  6. reviveearly

    reviveearly

    Joined:
    Feb 20, 2019
    Posts:
    13
    Oh I have another logical problem if you don't mind sticking around a little more.

    Code (csharp):
    1.  
    2. void Update()
    3.         {
    4.             if(myanim.speed >= 0.0f && Input.GetKeyDown(KeyCode.D))
    5.             {
    6.                 myRigidBody.velocity = new Vector3(moveSpeed, myRigidBody.velocity.y, 0f);
    7.                 myRigidBody.velocity = new Vector3(moveSpeed, myRigidBody.velocity.y, 0f);
    8.                 myanim.SetFloat("Speed", Mathf.Abs(myRigidBody.velocity.x));
    9.                 gameObject.GetComponent<Animator>().enabled = true;
    10.                
    11.             } else if (myanim.speed >= 0.0f && !Input.GetKeyDown(KeyCode.D))
    12.             {
    13.                 gameObject.GetComponent<Animator>().enabled = false;
    14.             }
    15.  
    The animation simply doesn't play. So I change it to:

    Code (csharp):
    1.  
    2.             } else if (myanim.speed >= 0.0f && !Input.GetKeyDown(KeyCode.D))
    3.             {
    4.                 myanim.Speed = 0.0f;
    5.             }
    6.  
    and the animation still won't play. I just need it to exit out of the animation when the D key is not being pressed. Thank you!
     
  7. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    491
    Never enable or disable the Animator, that will simply stop it from processing the animation state altogether and result in nothing playing.
    With this code you will only change the Animator's values when you press down the key for the first frame, after which next frame you will immediately turn the animator off:
    Frame 1, you press D and the animator's speed is 0 results in the first if statement being executed.
    Frame 2, you still have D down but since you use GetKeyDown instead of GetKey (see my first reply) it will return false, which becomes true due to the negation. Animator speed is also not 0, so the entire second if returns true and turns the animator off, resulting in you not seeing any animations at all.
    Besides that, you're just doing a lot of unnecessary stuff and checks here.

    The below code is how I would tackle this in simple terms. It will simply check for your movement keys at all times and update the animator's Speed float accordingly, so there's no way to get into edge cases.
    Code (CSharp):
    1. void Update()
    2. {
    3.     float moveInput = Input.GetAxis("Horizontal"); //Save whether or not a button for horizontal movement is pressed, typically A or D. Keys can be changed in Project Settings>Input Manager. -1 = A, 1 = D, 0 is None.
    4.     myRigidBody.velocity = Vector3.right * moveInput * moveSpeed; //Move in the direction you're actually pressing.
    5.     myAnim.SetFloat("Speed", Mathf.Abs(moveInput) * moveSpeed; //Doing this will make sure the animator will always have the correct speed value. MoveSpeed when you are holding a button, 0 if you're not holding anything. If you want to do a backwards walking animation, you can remove mathf.Abs and simply use moveInput * moveSpeed and drive the animation selection with negative values.
    6. }
    I don't want to be mean, but it looks like you're really overthinking your programming and making a lot of bad mistakes that way right now. I'd recommend learning some more first by just following simple tutorials like Brackeys' videos, and probably some extra reading up on basic C# scripting.
     
  8. reviveearly

    reviveearly

    Joined:
    Feb 20, 2019
    Posts:
    13
    Thanks for the advice and feedback! I'll take a look at his tutorials right now.