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

Two animations at once. Code problem

Discussion in 'Scripting' started by BringTheRuckus, Jan 3, 2020.

  1. BringTheRuckus

    BringTheRuckus

    Joined:
    Jun 9, 2019
    Posts:
    3
    Hey guys,

    I'm doing a 2D, 4 directional player based game. I have a problem with the idle animation playing the same time as the walk animation. I've tried searching and tried a few things that were suggested, but with no success. Most suggested was to blend the animation and let Unity make the transition for you, but I'm not able to get that to work. I've tried using myAnimator to set 2 different bools "isMoving" and "isIdle". Everything is activating in the animator when I run the game, but it's just the 2 animations on top of each other. I've tried the StopPlayback, didn't work. I tried myAnimator.ResetTrigger, didn't work. I'm starting to think it's the way I have my code set up. I try my best to figure things out for myself, but this one has me stuck. Here is code for the left idle to left walk. No need to worry about the curly brackets missing from FixedUpdate and the 1 if statement, they are in the full script.

    <script src="https://pastebin.com/embed_js/ApDtbWWP"></script>

    https://pastebin.com/ApDtbWWP
    Thanks for any insight.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    First I'm not sure what you're doing with inputs there. You need to find out what Input.GetButton("Horizontal") even does. I know doing a .GetAxis() on it gives you a number from -1.0 to 1.0, but I have no idea what the .GetButton() call on an analog input axis even does. Isolate it, print the result, wiggle the controls, figure out if that's your problem.

    If that's not your problem, make a new scene with your animation object in it. Press PLAY and then go into the animator window and change your properties. Do the animations work correctly under manual control? If they are broken there, fix them there, it has nothing to do with code.
     
  3. BringTheRuckus

    BringTheRuckus

    Joined:
    Jun 9, 2019
    Posts:
    3
    The Input.GetButton("Horizontal") is another way of using GetKeyDown for example except it's for multiple platforms. For example, there is no "Space" key on a mobile phone. That's what I understand it to be anyways.

    However, thank you for the reply but I just figured it out. I re-did my code and instead of using Booleans on the animator, I used floats and called those within the code. It wasn't switching from the idle animation so it was playing both.
     
    Kurt-Dekker likes this.