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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[Debugging] why is my velocity changing between Update() calls?

Discussion in 'Editor & General Support' started by inZania, Mar 17, 2016.

  1. inZania

    inZania

    Joined:
    Mar 3, 2016
    Posts:
    28
    I have a problem with my movement "stuttering."

    Code (csharp):
    1.  Vector3old=isoRigidBody.velocity;
    2. isoRigidBody.velocity=newVector3(2,0,0);
    3. Debug.Log(old+"->"+isoRigidBody.velocity+"("+Time.time+")");
    When I run this inside my update function, the log sometimes shows that the value is changing from some smaller value on some Update() calls, such as from (1.9,0,0) to (2,0,0).

    I don't have any physics on my object, so I can't figure out why this is happening. I'm still fairly new to Unity and don't know what the potential causes are. I wanted to figure out how to set a memory breakpoint in MonoDevelop (so I could find out who's modifying the velocity variable between frames), but I can't find an elegant way to do this.

    So, what's the best way to debug this issue?
     
  2. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,727
    You should try to not manually set the velocity of rigidbody. It produces unexpected results.
    The manual even warns you not to do it:
    http://docs.unity3d.com/ScriptReference/Rigidbody-velocity.html
    You should use something like:
    http://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html
     
  3. Zuntatos

    Zuntatos

    Joined:
    Nov 18, 2012
    Posts:
    612
    To be fair, it says unrealistic results, not unexpected results.
    @inZania can you try it while printing the vector elements manually? Like Debug.Log("Velocity = [{0}|{1}|{2}]", vel.x, vel.y, vel.z), as the vector3 -> string conversion does silly things to make it more readable.
     
  4. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,727
    It is unexpected results because if you set the velocity every update the rigidbody will eventually pass through walls and floors and other items with colliders, which is not expected behaviour of a rigidbody.
     
  5. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
    We do exactly that in Android game and see no issues. It's using RigidBody2D with gravity, physics and everything. You've had a programming mistake somewhere.

    For original post, maybe the player is tripping on edges of floor colliders? Even if player feet are circle collider, a Y difference of 0.001 makes a corner for 2 box colliders and can cause a slight change in speed.
     
    Last edited: Mar 20, 2016
  6. inZania

    inZania

    Joined:
    Mar 3, 2016
    Posts:
    28
    It turns out the materials were creating friction =P