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. Dismiss Notice

Locomotion System - Ungrouped Animations will not crossfade

Discussion in 'Scripting' started by rmgalante, Jun 18, 2012.

  1. rmgalante

    rmgalante

    Joined:
    Nov 21, 2009
    Posts:
    59
    Hello,

    I have integrated the locomotion system into my project and it is working fine. I want to integrate strafe left and strafe right animations. According to the documentation, one integrates strafe animations in the Ungrouped motion group. So I have, and I wrote a script that starts and stop the animations as specified in the JumpAndIdle script in the demo. I'm using the Q and E keyboard characters to start the strafe left and strafe right animations respectively. But the animations get stuck after the first frame and do not play. I release the Q or the E key, and the idle animation resumes.

    Here is the code.

    Code (csharp):
    1.  
    2. void LateUpdate()
    3. {
    4.     // Use the Q key to strafe left
    5.        
    6.     if (_doStrafeLeft)
    7.     {
    8.         if (Input.GetKey(KeyCode.Q)  !_wasStrafingLeft)
    9.         {
    10.             // Fade to strafeleft animation quickly
    11.             animation[StrafeLeftAnimation.name].time = 0;
    12.             animation[StrafeLeftAnimation.name].wrapMode = WrapMode.Loop;
    13.             animation.CrossFade(StrafeLeftAnimation.name, 0.1f);
    14.             _wasStrafingRight = false;
    15.             _wasStrafingLeft = true;
    16.         }
    17.         else if (_wasStrafingLeft  !Input.GetKey(KeyCode.Q))
    18.         {
    19.             // Crossfade to locomotion
    20.             animation.CrossFade(MotionGroup);
    21.             _wasStrafingLeft = false;
    22.         }
    23.     }
    24.        
    25.     // Use the E key to strafe right
    26.        
    27.     if (_doStrafeRight)
    28.     {
    29.         if (Input.GetKey(KeyCode.E)  !_wasStrafingRight)
    30.         {
    31.             // Fade to straferight animation quickly
    32.             animation[StrafeRightAnimation.name].time = 0;
    33.             animation[StrafeRightAnimation.name].wrapMode = WrapMode.Loop;
    34.             animation.CrossFade(StrafeRightAnimation.name, 0.1f);
    35.             _wasStrafingLeft = false;
    36.             _wasStrafingRight = true;
    37.         }
    38.         else if (_wasStrafingRight  !Input.GetKey(KeyCode.E))
    39.         {
    40.             // Crossfade to locomotion
    41.             animation.CrossFade(MotionGroup);
    42.             _wasStrafingRight = false;
    43.         }
    44.     }
    45. }
    46.  
    I press the Q key and the first frame of the animation plays and then it freezes. I release the Q key and the idle animation resumes. Same symptom with the E key.

    Does anyone have a suggestion? Has anyone implemented strafing logic with the locomotion system?
     
  2. homeros

    homeros

    Joined:
    Dec 23, 2009
    Posts:
    121
    Maybe use GetKeyDown instead of GetKey. Because the way I see it at the momeny you just start the animation over and over while the key is pressed.
     
  3. rmgalante

    rmgalante

    Joined:
    Nov 21, 2009
    Posts:
    59
    Thanks for your reply.

    The flags, _wasStrafingLeft and _wasStrafingRight, prevent the condition to which you refer. The animation is not starting over and over. I placed a debug statement inside anyway to prove that this is not the case.

    GetKeyDown will not resolve the issue either.

    Rob