Sprite scale and world scale

Hello everyone,

Searching for hours now. I basically want to scale a sprite ( Unity 4.3 ) by setting a world pos.

I’m searching for proportinal values but can’t obtain something satisfying.

So basically I want something like gameObject.transform.localScale.x = 228 * ratio

Thanks in advance for any maths help :slight_smile:

Edit:

Further explanations:

I have a simple Cube created in hierarchy which has a scale x and y of 45.

I have a sprite created from a texture ( 128 x 64 ) that I want to be exactly as big as the cube ( so 45 x 45 in world pos )

How can I, in code, calculate the ratio between the 100 x 100 values of the sprite scale in the inspector to the 45 x 45 worldspace pos that I want?

My sprites are in 100 pixelsToUnits.

My camera ortho size is half my screen height, so 500 actually.

You can get the world size of your sprite from the Sprite.bounds. You can divide that size by your target size to get the size to set Transform.localScale. So if we wanted to set a sprite to be 45 units wide, we could do:

#pragma strict
 
function Start () {
	var bounds : Bounds = GetComponent(SpriteRenderer).sprite.bounds;
	var xSize = bounds.size.x;
	transform.localScale.x = 45.0 / xSize;	
}