Why 0.5 * width?

Hello there,

Why we use * 0.5 in
float rightEdgeOfFormation = this.transform.position.x + (0.5f * width);
float leftEdgeOfFormation = this.transform.position.x - (0.5f * width);

Why 0.5f ?

Thanks

There are two reasons.

  1. Position of transform.position.x returns the center of an object of that plane (in this instance the x plane). 0.5f divides the width by half. By adding or subtracting that number to the current transform.position.x location you get either the right most edge or left most edge of the object at that moment in time.

Say the width of the object is 500 pixels and the current transform.position.x is at 600 pixels.
If you divide the width by half you get 250 pixels. This is the center of the x plane of that object. Now if you add that number to the current transform.position.x , You get 850 pixels. That it the current position of the right most edge of the object. If you do the same but subtract you get 350 pixels. That is the left most edge.

Assuming transform.position.x is currently at 600 pixels and the width of the object you are manipulating is 500 pixels wide.
Right Edge = transform.position.x + (0.5f * width)
850 = 600 + (0.5f * 500)
850 = 600 + 250

Left Edge = transform.position.x - (0.5f * width)
350 = 600 - (0.5f * 500)
350 = 600 - 250

  1. The reason he used 0.5f * width instead of say width / 2 is because the division operation is more CPU intensive than using multiplication.
1 Like

Privacy & Terms