Search Unity

Animation Back and Forth

Discussion in 'Animation' started by SmorgasOfBorg, Feb 17, 2017.

  1. SmorgasOfBorg

    SmorgasOfBorg

    Joined:
    Mar 1, 2015
    Posts:
    5
    Hey all, been visiting the forums for a long time now. Pretty sure this is my first post though.

    I'm having tons of fun working with the Animation system, but I feel like I might not be using it correctly, because I am having an issue that seems like it should be easier than I am making it out to be.

    I am trying to animate my tank. Very simple. I want to move it forward a bit and back a bit, and of course a position in the middle. So 3 positions.

    I have an Animation for Forward and for Back. They control transform position, as well as the rotation of the wheels, and even the scrolling of the tread texture. No problems with the animations themselves. And I have them as states in an Animator that runs them on Triggers. After running one, it immediately goes back to an Idle state that does nothing.

    My problem is the start points for each of them. Because they go back to idle afterwards, the positions and rotations would all get reset at the end of the animation. I solved the position problem, by running a behaviour after the animation that would update the position of the parent to the position of my animated child (the animator is a child of a GameObject). Then when the animated child got reset back to zero (when it went back to Idle), it didn't matter because the parent had moved to catch up with it. I later read that I could have avoided doing this by simply unchecking the Write Defaults checkbox on the Idle state. But doing that doesn't help with the next problem, which is my reason for writing this up:

    When the next animation starts (like I just ran the Forward animation, and now I want to run the backwards one). The start point of Backward is not the same as the last position of Forward. Again, the behaviour addresses fixing this position issue. But the wheel rotations and tank tread scroll are another story. I don't know how to get them to retain their position after the animation. When I MoveForward, I rotate the wheel from 0 to 60deg. And when I MoveBackward, I rotate the wheel from 0 to -60deg. You can probably see my problem.

    I feel like I am just missing something easy and am doing this wrong. I was hoping some of you guys can just slap me awake and tell me how easy this SHOULD be.

    Thanks!
     
    fastgamedev likes this.
  2. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    Hi,

    You're use case is exactly the one I took to implement the state properties feature.
    You only need one animation 'forward' and change the speed multiplier of this animation.

    You will first need to add a float parameter in your controller, call it something like tankSpeed and set a default value to 1.
    Then on your state that play the forward animation, Check Speed Multiplier Parameter and choose your parameter tankSpeed.

    Load this package into unity and open scene tank, press play, in the scen view you should see a vertical slider, move it to see the result on the cube.

    With this you are always gurantee that the animation match backward and forward.
     

    Attached Files:

  3. SmorgasOfBorg

    SmorgasOfBorg

    Joined:
    Mar 1, 2015
    Posts:
    5
    Thanks for the response. However, this solution seems to be for continuous locomotion. Great solution for that problem. But I am not wanting that. I don't want to loop the movement. When I move forward, I want to move forward a pre-determined distance (as setup in the clip), and then stop and sit there until I get another move command. If I receive a command to move back, I want to move back a pre-determined distance and then stop. But I want to start with the same position, and individual rotations, where i left off in the previous animation. It seems triggers are more appropriate for that.

    I suppose if I had to, I could put a behavior on the state and kill the speed variable when it ends. Some of this is just me thinking out loud too. Sorry.
     
  4. SmorgasOfBorg

    SmorgasOfBorg

    Joined:
    Mar 1, 2015
    Posts:
    5
    Perhaps overthinking it. Maybe I should just create 3 states, Forward, Middle, Back. And just manage the transiitons between them?