Hello Guest

Author Topic: how to determine the exact position when instantiating a prefab.  (Read 9279 times)

JerryCic

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 11
    • View Profile
how to determine the exact position when instantiating a prefab.
« on: February 12, 2012, 01:19:49 am »
Hi
 
I have a gameobject called "Staff".
It is the parent of one prefab called "spGClef".
When i click on a button, i want to make the staff into one that has 6 children. 1 "spGClef", 4 "spMeasure", and 1 "spEnd".
I can do all that quite well.
What i cannot seem to do is get the positions as perfect end to end arrangement.
My sprite collections have "Target Height"=480.
My camera scale is 1,1,1 ortho size=1.
From Trial and error i have discovered that one pixel=.004166666 in the scale of x or y
Since the width of my "spGclef" is known to be 120, then my first measure must be placed 120 pixels to the right of the "spGClef" object. That is (.004166666 * 120)=0.5.

1) how can i determine the exact width of my sprite relative to the screen.width in pixels?
2) how can i determine mathematically the magic number (.00416666 in my case) so that i can get exact end to end placement of sprites dynamically at runtime?

EDIT: I also need to add a box colider to the resulting collective object so i need to know the final size for the collider.

Thank you up front.
Jerry C
« Last Edit: February 12, 2012, 01:36:29 am by JerryCic »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: how to determine the exact position when instantiating a prefab.
« Reply #1 on: February 12, 2012, 01:54:45 am »
You can use Camera.ScreenToWorldPoint and Camera.WorldToScreenPoint to work out both those values exactly without any guesswork. It doesn't matter what your orthosize or target height is set to. Simply pass screen coordinates to Camera.ScreenToWorldPoint - the only gotcha is the z component is the distance from the camera to your sprite.

Hope that helps,
unikron

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: how to determine the exact position when instantiating a prefab.
« Reply #2 on: February 12, 2012, 01:55:55 am »
The latest version will have a nicer solution to this. A working beta has been posted here, if you would like to check it out.
http://unikronsoftware.com/2dtoolkit/forum/index.php/topic,52.msg92.html#new


JerryCic

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: how to determine the exact position when instantiating a prefab.
« Reply #3 on: February 12, 2012, 02:25:08 am »
Thank you for your response

I still cant figure out how to retrieve the width of my sprite.
The scale is 1,1,1 and is the same no matter what the size of the sprite.
my "spGClef" scale is 1,1,1
my "spMeasure" scale is 1,1,1
but the two sprites are 120 pixels wide and 560 pixels wide respectivly
Is there a way that i can determine the width of the sprite in pixels at runtime?
Inside the program, i can only see/set the scale and position.

Thanks
Jerry

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: how to determine the exact position when instantiating a prefab.
« Reply #4 on: February 12, 2012, 06:33:18 pm »
I've posted a reply to your other thread - I believe I answer this question there as well!

unikron