- Home /
Instantiate a sprite inside bounds of other sprite?
I want to instantiate sprite inside bounds of custom shaped sprite(not box or circle). I tried using an "other" object and collider to check the "other" object positions are inside the bounds but it takes time and memory.so the game slows down. does anyone know a better way to solve this problem?
Answer by tebandesade · May 13, 2015 at 07:30 PM
Hi how are you? I don't know when you say "other" object you mean to not have a reference at all of the object, if you do, well you can calculate the bounds of the sprite by creating an empy game object and getting the coordinates where you want it to be instantiate it . Example . Get the start x point, get the end x point , Y start, Y end (all done manually by placing the empty game object you create it to check where these coordinates are in the world ) and later divide both end and start to get the center of the X and Y bound and use these in your new Vector3() you have to place in the instante function.
Otherwise, if you can reference, get the reference of the renderer of the sprite and later
Bounds ti = Object.GetComponent<Renderer>().bounds;
Vector3 ola = ti.center;
Instantiate (test, ola ,transform.rotation );
With this code, you'll get an object of the same size that always instantes itself.
Static
dynamic
Object being the object that has the bounds and test the prefab you're instantiating. Important things to take in to account, the object being instantated has to be the same or less size than the object that has the bounds, if not, you'll have to scale it.
More information regarding how to calculate bounds http://docs.unity3d.com/ScriptReference/Bounds.html
Your answer
![](https://koobas.hobune.stream/wayback/20220613195500im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Instantiating prefabs within the MeshFilter volume of object 1 Answer
Instantiated Sprite is not rendering. 1 Answer
How can I detect non-rectangular bounds? 0 Answers
How do I get the bounds of a sprite on its sprite atlas? 0 Answers
Rotating a sprite toward the mouse position on instantiation. 0 Answers