Search Unity

Rigidbody2D.velocity inside FixedUpdate()

Discussion in '2D' started by noio, Jan 28, 2015.

  1. noio

    noio

    Joined:
    Dec 17, 2013
    Posts:
    230
    I just upgraded to the Unity 5 beta, to check out it's features. As a result, my animation system is now broken.

    It looks like in Unity 5 that the new Entry node is causing the animation to constantly transition to the default node. Is there a way to work around this?



    I just don't entirely understand when/how the Entry node transition is used.

    I understand that the problem lies somewhere else (below):

    My animation code is somewhat like this:

    Code (CSharp):
    1. AnimationClip walkAnimation
    2. AnimationClip stopAnimation
    3.  
    4. void FixedUpdate()
    5. {
    6.   Debug.Log( rigidbody2d.velocity.x );
    7.  
    8.   // Check if the character was stopped by, e.g., a wall
    9.   if ( rigidbody2d.velocity.x < 0.01f )
    10.   {
    11.     _animator.Play(stopAnimation.name);
    12.   }
    13.   else
    14.   {
    15.     _animator.Play(walkAnimation.name);
    16.   }
    17.   Vector2D targetVelocity = new Vector2( 2.2f, 0 );
    18.   rigidbody2d.velocity = targetVelocity;
    19. }
    The printed velocity is not "2.2" each frame, as you would expect, but actually something like:

    Code (CSharp):
    1. Velocity: 2.0
    2. Velocity: 0
    3. Velocity: 0
    4. Velocity: 0
    5. Velocity: 2.0
    6. Velocity: 0
    7. Velocity: 2.0
    8. ...etcetera
    Because the animation code detects whether the character is "stopped", the animation goes nuts:


    Why is the reported velocity "0" in so many frames?!
     
    Last edited: Jan 29, 2015
  2. noio

    noio

    Joined:
    Dec 17, 2013
    Posts:
    230
    This should probably go in the Physics or the Unity 5 board. Apologies, I didn't go through the list. Could a mod move it, please?
     
    Last edited: Jan 29, 2015
  3. noio

    noio

    Joined:
    Dec 17, 2013
    Posts:
    230
    I did some more debugging. I replaced my entire animation code with the following simple component:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Motion : MonoBehaviour {
    5.  
    6.     // Use this for initialization
    7.     void Start () {
    8.    
    9.     }
    10.  
    11.     void Update()
    12.     {
    13.         Debug.Log ("Update  " + Time.deltaTime);
    14.     }
    15.    
    16.     // Update is called once per frame
    17.     void FixedUpdate () {
    18.         Debug.Log ("Fixed  " + Time.time);
    19.         Debug.Log ("Velocity: " + GetComponent<Rigidbody2D>().velocity );
    20.         GetComponent<Rigidbody2D>().velocity = new Vector2(0.5f, 0);
    21.     }
    22. }
    23.  
    And I logged the output. Every time after a regular Update() occurs, the velocity in the next FixedUpdate() is 0. What is this I don't even?

     
  4. noio

    noio

    Joined:
    Dec 17, 2013
    Posts:
    230
    Oh so it was the Animator! It was stopping my objects even though the transform isn't animated! Is this a U5 bug?

     
    tenshi47 likes this.
  5. SuperNoctiz

    SuperNoctiz

    Joined:
    Jan 24, 2014
    Posts:
    20
    Could you try do a further verification instead of only else? Here:

    Instead of:
    Code (CSharp):
    1.  else
    2.   {
    3.     _animator.Play(walkAnimation.name);
    4.   }
    Do:
    Code (CSharp):
    1.  else if ( rigidbody2d.velocity.x > 0.01f )
    2.   {
    3.     _animator.Play(walkAnimation.name);
    4.   }
    I honestly dunno if that could help, but it creeps me out that sometimes the else state covers a lot more than what we want.


    Or did you already fix it? :D
     
  6. noio

    noio

    Joined:
    Dec 17, 2013
    Posts:
    230
    Yes, the Apply Root Motion checkbox did the trick.
     
  7. SuperNoctiz

    SuperNoctiz

    Joined:
    Jan 24, 2014
    Posts:
    20
    Oh alright then, nice :) Good luck with the rest