Hello Guest

Author Topic: Still can't place a sprite in the right place.  (Read 13916 times)

JerryCic

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 11
    • View Profile
Still can't place a sprite in the right place.
« on: February 12, 2012, 05:57:37 am »
Hi

Sorry to be so confused at such a simple task.

I have a 800x480 sprite placed at 0,0,10
I have a 120x400 sprite i want to place so that the lower left corner matches the lower left corner of the larger sprite.
my camera is ortho size=1
my target height is 480

I have tried to use the ScreenToWorld and visa versa but cannot get it to line up.
Everything has pixel perfect set up.

I have tried this:
           
            // find the center of the screen and large (background) sprite
            Vector3 v = myCamera.WorldToScreenPoint(new Vector3(0,0,0));
            // move 340 pixels to the left of center
            // move 40 pixels down from center
            Vector3 s = new Vector3(v.x - 340, v.y - 40, v.z);
            GameObject.Find("Staff").transform.position = s;

but it places it too far to the left like about 40 pixels too far.
The y coordinate looks ok.
What am i doing wrong?

Thanks
Jerry           

nikolic

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 48
    • View Profile
Re: Still can't place a sprite in the right place.
« Reply #1 on: February 12, 2012, 03:48:40 pm »
What's your smaller sprite's anchor setting?

JerryCic

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Still can't place a sprite in the right place.
« Reply #2 on: February 12, 2012, 04:54:46 pm »
Thanks for responding

Both sprites have anchor settings as dead center.



unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Still can't place a sprite in the right place.
« Reply #3 on: February 12, 2012, 06:31:24 pm »
Hi,

You can get this by doing the following:
Code: [Select]
// position first sprite at 100, 245, and 10 units away from the camera
sprite.transform.position = cam.ScreenToWorldPoint(new Vector3(100, 245, 10));

GameObject newSprite = GameObject.Instantiate(sprite.gameObject) as GameObject;

// get untrimmed bounds of this object
Bounds bounds = sprite.GetUntrimmedBounds();

// you can do this if the objects have the same dimensions
newSprite.transform.position += new Vector3(bounds.extents.x * 2.0f, 0, 0);

// but really what you need is
//Vector3 secondBounds = newSprite.sprite.GetUntrimmedBounds();
//newSprite.transform.position += new Vector3(bounds.extents.x + secondBounds.extents.x, 0, 0);



If you want to position a sprite (M,N) pixels away from another sprite, simply do:
Code: [Select]
Vector3 screenPosition = cam.WorldToScreenPoint(firstSprite.transform.position);
secondSprite.transform.position = cam.ScreenToWorldPoint(screenPosition + new Vector3(M, N));

Using the first method is much more reliable, as it will still work when the sprite sizes change.

Cheers,
unikron

JerryCic

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Still can't place a sprite in the right place.
« Reply #4 on: February 12, 2012, 08:30:28 pm »
Hi and thanks

I'm sure the answer is in there somewhere but!!!!

GetUntrimmedBounds() does not seem to exist as a function in my version on Unity.

Any ideas?

Also i cannot find any documentation in the unity reference manual


unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Still can't place a sprite in the right place.
« Reply #5 on: February 12, 2012, 09:09:50 pm »
Thats a function in 2D Toolkit - it is a function in tk2dSprite.
http://www.unikronsoftware.com/2dtoolkit/doc/classtk2d_sprite.html

what version of 2D Toolkit are you using?

JerryCic

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Still can't place a sprite in the right place.
« Reply #6 on: February 12, 2012, 10:14:35 pm »
Hi

i have 1.56final

your example instantiates a sprite
my sprite is already instantiated.

I can find it as a game object
GameObject myBackground =  GameObject.Find("spBackground");

how do i find it as a tk2dSprite?
What is the equivalent of this?
tk2dSprite myBackground=tk2dSprite.Find("spBackground");

so that i may expose the functions in question.

EDIT this is your code:
************************************************
   // position first sprite at 100, 245, and 10 units away from the camera
       sprite.transform.position = cam.ScreenToWorldPoint(new Vector3(100, 245, 10));
      
       GameObject newSprite = GameObject.Instantiate(sprite.gameObject) as GameObject;
      
       // get untrimmed bounds of this object
       Bounds bounds = sprite.GetUntrimmedBounds();
      
       // you can do this if the objects have the same dimensions
       newSprite.transform.position += new Vector3(bounds.extents.x * 2.0f, 0, 0);
      
       // but really what you need is
       //Vector3 secondBounds = newSprite.sprite.GetUntrimmedBounds();
       //newSprite.transform.position += new Vector3(bounds.extents.x + secondBounds.extents.x, 0, 0);
*****************************************

"newSprite" is clearly a GameObject. The lines after the (//but really) imply
that GetUntrimmedBounds() returns a Vector3 but I get an error
Also what is "newSprite.sprite" refferring to? sprite is not a property of GameObject.


Thanks
 Jerry
« Last Edit: February 12, 2012, 10:46:56 pm by JerryCic »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Still can't place a sprite in the right place.
« Reply #7 on: February 12, 2012, 11:11:29 pm »
if you are using c#, and assuming you have your GameObject in myBackground...
tk2dSprite myBackgroundSprite = myBackground.GetComponent<tk2dSprite>();


JerryCic

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Still can't place a sprite in the right place.
« Reply #8 on: February 13, 2012, 01:09:14 am »
Hi

when i execute this line:

Bounds bounds=myBackgroundSprite.GetUntrimmedBounds();

I get an error. Array index out of range
The line of code is the return line in the Bounds definition

   public Bounds GetUntrimmedBounds()
   {
      var sprite = collection.spriteDefinitions[_spriteId];
      return new Bounds(new Vector3(sprite.untrimmedBoundsData[0].x * _scale.x, sprite.untrimmedBoundsData[0].y * _scale.y, sprite.untrimmedBoundsData[0].z * _scale.z),
                        new Vector3(sprite.untrimmedBoundsData[1].x * _scale.x, sprite.untrimmedBoundsData[1].y * _scale.y, sprite.untrimmedBoundsData[1].z * _scale.z));
   }

Jerry

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Still can't place a sprite in the right place.
« Reply #9 on: February 13, 2012, 01:22:12 am »
Could you recommit your sprite collection and try again? Looks like the bounds data isn't there for some reason...

JerryCic

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Still can't place a sprite in the right place.
« Reply #10 on: February 14, 2012, 02:02:08 am »
Yes!

That did it! I can now achieve perfection.

Thank you for your time.

JerryC