Search Unity

Controlling animation playback

Discussion in 'Animation' started by Deleted User, May 23, 2015.

  1. Deleted User

    Deleted User

    Guest

    Hello all. I'm basically Unity beginner, but I like it a lot.
    Now I need help with steering wheel animation. I'm working on little driving project and I want to animate steering wheel. I was thinking about how to do this and ended up with animation. I have vehicle model (created in blender) with steering wheel animation (and other parts animations). I also have steering wheel value in range from 0.0f to 1.0f (0.5f for straight direction). So my plan was to have paused steering wheel animation and set its normalized time of animation depending on steering value. But I don't know how to do that. I'm using "Animator" with differrent layers for different parts, so those parts can be animated independently. One shot animations works fine, but how to set specific time of animation and pause? I was trying to use this:

    Code (CSharp):
    1. m_Animator.Play(_AnimName, m_Animator.GetLayerIndex(_Layer), _Time);
    This sets correct animation time, but animations keeps playing from this time and i don't know how to pause it. Maybe "Animation" is better for my purpose....??
     
  2. Griffo

    Griffo

    Joined:
    Jul 5, 2011
    Posts:
    700
    You could do something like this ..

    lerp the value from what it is when called to what you want it to be, middle, left or right, in the time you want, slowly going round a bend or fast going round a hairpin ..

    Code (CSharp):
    1.  
    2. #pragma strict
    3.  
    4. var testLerpSpeed : float = 2;
    5.  
    6. private var i : float;
    7. private var _animator : Animator;
    8. private var incomingSteeringWheelValue : float;
    9.  
    10. function Start ()
    11. {
    12.     _animator = GetComponent(Animator);
    13. }
    14.  
    15. function Update () {
    16.     if (Input.GetKeyDown ("1"))
    17.     {
    18.         wheelCenter();
    19.     }
    20.     if (Input.GetKeyDown ("2"))
    21.     {
    22.         wheelLeft();
    23.     }
    24.     if (Input.GetKeyDown ("3"))
    25.     {
    26.         wheelRight();
    27.     }
    28. }
    29. //              ----------------------------
    30. function wheelCenter ()
    31. {
    32.     i = 0;
    33.     incomingSteeringWheelValue = _animator.GetFloat("SteeringWheelValue");
    34.     while(i < 1)
    35.     {
    36.         // i goes towards 1
    37.         i += Time.deltaTime/testLerpSpeed;
    38.         // keep i between 0 and 1
    39.         i = Mathf.Clamp(i, 0, 1);
    40.         _animator.SetFloat("SteeringWheelValue", Mathf.Lerp(incomingSteeringWheelValue, 0.5, i));
    41.         yield;
    42.     }
    43. }
    44. //              ----------------------------
    45. function wheelLeft ()
    46. {
    47.     i = 0;
    48.     incomingSteeringWheelValue = _animator.GetFloat("SteeringWheelValue");
    49.     while(i < 1)
    50.     {
    51.         // i goes towards 1
    52.         i += Time.deltaTime/testLerpSpeed;
    53.         // keep i between 0 and 1
    54.         i = Mathf.Clamp(i, 0, 1);
    55.         _animator.SetFloat("SteeringWheelValue", Mathf.Lerp(incomingSteeringWheelValue, 0.0, i));
    56.         yield;
    57.     }
    58. }
    59. //              ----------------------------
    60. function wheelRight ()
    61. {
    62.     i = 0;
    63.     incomingSteeringWheelValue = _animator.GetFloat("SteeringWheelValue");
    64.     while(i < 1)
    65.     {
    66.         // i goes towards 1
    67.         i += Time.deltaTime/testLerpSpeed;
    68.         // keep i between 0 and 1
    69.         i = Mathf.Clamp(i, 0, 1);
    70.         _animator.SetFloat("SteeringWheelValue", Mathf.Lerp(incomingSteeringWheelValue, 1.0, i));
    71.         yield;
    72.     }
    73. }
    74. //              ----------------------------
     
    Last edited: May 24, 2015
  3. Deleted User

    Deleted User

    Guest

    First of all thank you for your reply. Now about your suggestion... If I'm right, user can set only three steering wheel states (left, center, right) and your functions continuously animates steering wheel to this position. Right? But I need something little bit different. Steering wheel value can be changed to any value in range from 0 to 1. For example when player will use joystick or steering wheel controller. So when the joystick is set to e.g. 0.7f, animation of steering wheel must be set to this normalized time immediately and stay paused at this time. I hope you understand what I'm trying to explain (sry for my english).
     
  4. Griffo

    Griffo

    Joined:
    Jul 5, 2011
    Posts:
    700
    Not sure how you would but in Update() just read the input of the joystick 0 - 1 then set the float in the animator to match ..

    Code (JavaScript):
    1. private var joystickInput : float;
    2. private var _animator : Animator;
    3.  
    4. function Start ()
    5. {
    6.     _animator = GetComponent(Animator);
    7. }
    8.  
    9. function Update ()
    10. {
    11.     joystickInput = // read joystick input value of 0 to 1
    12.    
    13.     _animator.SetFloat("SteeringWheelValue", joystickInput);
    14. }
     
  5. Deleted User

    Deleted User

    Guest

    Ok. I will check it out and let you know soon.... Again, thank you for your interest.
     
  6. Deleted User

    Deleted User

    Guest

    Ok, but what in Animator? I will add property "SteeringWheelValue" in Animator, but how to setup Animator? I have one state (clip). Set this state as default? And how to setup this "SteeringWheelValue" property? Thank you ;)