Search Unity

Having trouble controlling animation time

Discussion in 'Scripting' started by petey, Aug 22, 2009.

  1. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,824
    Hi there,
    I'm trying to script the animation of a character leaning, as the player steers left and right.
    This small bit of code seems to be doing the job but the character is a little jittery,
    it looks as though it's trying to play each frame then it gets to the
    Dude.animation["lean"].normalizedTime = (horizontalvalue*.5) -.5; part and corrects itself.

    I realise the game animation is set to play mode at the beginning of this script but if I have it set to stop, the animation won't move to the right frame in the Update function...

    Just wondering if I am perhaps doing something horribly wrong with this script.



    Code (csharp):
    1.  
    2. var Dude : GameObject;
    3. var horizontalvalue : float = 0;
    4.  
    5. function Start (){
    6. Dude.animation.Play("lean");
    7. }
    8.  
    9. function FixedUpdate () {
    10. horizontalvalue = -(Input.GetAxis ("Horizontal"));
    11. Dude.animation["lean"].normalizedTime = (horizontalvalue*.5) -.5;
    12. }
     
  2. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    i'd have to guess the animation is out of sync with the rendered frame because fixed update fires on it's time setting not each frame. put it in update instead of fixed update.

    [edit this: the way your calcing values actually might lead to some weird values too. I'd debug that one if it still acts up. something more like Input.GetAxis("Horizontal") * Time.deltaTime * speed might give you better results.]
     
  3. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,824
    hmm, still having trouble with this one...
    I've switched it just to Update now instead of fixed update but it still seems to get jittery, especially when it's under load (definitely better though).
    To me it seems that it must be to do with the fact that it is in play mode and the script keeps pulling it back into place.
    But if i set the animation to stop, it wont scrub through the frames :(

    I uploaded a simple version. If anyone has the time, check it out.

    Thanks
    Pete
     

    Attached Files:

  4. goodhustle

    goodhustle

    Joined:
    Jun 4, 2009
    Posts:
    310
    I was dealing with something similar; try setting your animation speed to 0.0 manually.

    For me, having a Blended mode animation that I wanted to manually control via normalizedTime was getting automatically played when i set the enable bit. The other weird thing I noticed is that subsequent calls to other character animations with PlayMode.StopAll or equivalent would stop the Blended animation - so unfortunately my current hack is to call Play on it every frame while speed is zero. Not sure if that's very performant but that's what I've got so far. HTH.

    -Gordon
     
  5. goodhustle

    goodhustle

    Joined:
    Jun 4, 2009
    Posts:
    310
    Oh, and also, you probably want to keep a desired normalizedTime value that you adjust via Mathf.Lerp over every frame, in order to smooth the inputs somewhat. That helped me smooth things out somewhat.
     
  6. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,824
    Thank you!
    That's fixed it :)
     
  7. goodhustle

    goodhustle

    Joined:
    Jun 4, 2009
    Posts:
    310
    Cool, glad to hear that a newbie like me could help! :)