when calculating speed in unity

    public float speed = 0;

Vector3 lastPosition = Vector3.zero;

 void FixedUpdate()
     speed = (transform.position - lastPosition).magnitude;
     lastPosition = transform.position;
It all goes well... However when increasing the drag value of the objects rigidbody component to 100 this speed value goes haywire! I have made a physics based movement system using the addforce rigidbody function to move along whichever local axis and used drag as a replacement for ground friction when walking along a surface becuase physics materials didnt work

How can I get a proper speed reading?

  • 2
    \$\begingroup\$ Rigidbody(2d) has velocity property. \$\endgroup\$
    – Nikaas
    Jan 6 at 10:44
  • \$\begingroup\$ How would i write that? I am in a unity 3d scene... i did: float speedInUnitPerSecond = GetComponent<Rigidbody2D>().velocity.magnitude; but im using a 3d rb not 2d... and im not sure how to calculate last position using vector 2... \$\endgroup\$
    – Object
    Jan 6 at 23:55
  • \$\begingroup\$ Just use the 3d one - float speedInUnitPerSecond = GetComponent<Rigidbody>().velocity.magnitude; \$\endgroup\$
    – Nikaas
    Jan 7 at 6:50


You must log in to answer this question.

Browse other questions tagged .