Position a Sprite in World Space

I’ve got a perspective camera with a 3D scene. In the scene is just a terrain plane at position (-1000, 0, 0). I’d like to manually position some sprites in this scene so that they are standing on top of the plane.

I’m using pre-positioned 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 as are the spawn points. 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.

I’ve tried positioning them with this code but they aren’t placed correctly.

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

var newPoint = new Vector3(transform.position.x, transform.position.y + demBounds.size.y), transform.position.z);

transform.position = newPoint;

When this code runs larger sprites are positioned higher than smaller but none are placed correctly. I’m guessing I’m missing some multiplier to offset the bounds of the SpriteRenderer but I have no idea.

One extra bit of info that might be useful is that if I put the sprites at Y = 0 the sprite is in a noticeably different position than the terrain (which is also at Y = 0). That strikes me as odd. The tops of the sprite are just a bit below the terrain.

Any help is much appreciated.

Thanks!

Here’s an update showing sizes and distances to further illustrate the problem.

[35304-positioning+problem.jpg|35304]

If you go back to the texture you are using for your sprite, you can change the pivot point to ‘Bottom’ and avoid doing this calculation.

Assuming your pivot is in the center, you want to use bounds.extents, which are 1/2 of the size.