how to measure the speed of a 2D object in unity.

Hello I’m a 2D developer and I need to find out the speed of my enemy. Thank you for any help

If you are using a RigidBody then getting the magnitude of the velocity will give you your speed in units/s. Its up to you to do a conversion from game units to real world units.

If you are not using RigidBody, then you need to do it over time to figure it out. Velocity = Distance / Time. This will also be in units/s.

Thank you, but can you write the code? my enemy uses rigodboby at a constant speed of 6 ,I need to determine the speed

You are setting it specifically to 6 units/s and you have a conversion of 1 meter = 1 unit then at 6 units/s its easily converted to 6 m/s. Its just math.

1 m = 3.3 feet so then 6 * 3.33 is 19…69feet/s.

There is 3600 seconds in an hour. So based on that if we travel at 6 m/s then that converts to 21.6 km/h.

At ~1.6 km/h per 1 mile/h: 6 m/s is 34.56 miles/h.

Thank you I wrongly asked a question I need to measure the speed during the game so that if it was equal to zero the enemy could turn around. I only need the 2D speed measurement code.

float speedInUnitPerSecond = rigidbody2D.velocity.magnitude;
3 Likes