Search code examples
c#unity-game-engine3d

Unity 3d C# player movement difference


What is difference between these two lines of codes?

rb.velocity = new Vector3 (rb.velocity.x, 5f,rb.velocity.z);

rb.velocity = new Vector3 (0, 5f,0);

both codes are in the this logic (if (Input.GetButtonDown("Jump"))


Solution

  • In the first case you specify that velocity of your rigidbody should retain x and z value, and set y component of velocity to 5.

    In the second example, you set velocity to the numbers you given.

    If before setting value x and z values of rb.velocity were 0, both statements will do the same.