Positioning a Sprite in WorldSpace

I’ve got a perspective camera with a 3D scene. In the scene is just a terrain plane. I’d like to manually position some sprites in this scene so that they are standing on top of the plane. The sprites are at different z values so some are larger in appearance than others.

I’ve tried positioning them with this code but I really am not sure at all about what I am supposed to be doing. I’m looking at details and haven’t gotten an idea for big picture stuff first.

var demBounds = gameObject.GetComponent<SpriteRenderer>().bounds;

gameObject.transform.position = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y + gameObject.GetComponent<SpriteRenderer>().bounds.extents.y, gameObject.transform.position.z);

I’m using prepositioned game objects as spawn points to take care of the X and Z for positioning so I only have to worry about the Y. The terrain is at Y position 0. So in this code I was trying to get a value equal to half the height of the sprite and use that number to offset the sprites position from 0. The sprite, however doesn’t move at all.

Any help is much appreciated.

Thanks!

Oh, here’s a screenshot if it is helpful to anyone.

[35292-sprite+not+flush.jpg|35292]

You are actually not setting any position. To set a position you either need to set a transforms position or localPosition. TransformPoint doesn’t set either of them, it just takes a Vecotr3 in local space as parameter and returns a world space. (Since you don’t use the return value it doesn’t do anything at all) :stuck_out_tongue:

I expect that what you want to do is something like this:

transform.position = new Vector3(transform.position.x, transform.position.y + GetComponent<SpriteRenderer>().bounds.extents.y, transform.position.z);

Or something like this, which does the same but is more clean.

transform.localPosition = new Vector3(0, GetComponent<SpriteRenderer>().bounds.extents.y, 0);

(The latter one only works if it is a child of the GameObject that represents the spawn location)

Why not change the pivot position of your sprites?
When you select the sprite, in the Inspector tab you have this option. If you set it to bottom you don’t have to do any coding …