2D Toolkit Forum
2D Toolkit => Support => Topic started by: JerryCic 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
-
What's your smaller sprite's anchor setting?
-
Thanks for responding
Both sprites have anchor settings as dead center.
-
Hi,
You can get this by doing the following:
// 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:
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
-
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
-
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?
-
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
-
if you are using c#, and assuming you have your GameObject in myBackground...
tk2dSprite myBackgroundSprite = myBackground.GetComponent<tk2dSprite>();
-
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
-
Could you recommit your sprite collection and try again? Looks like the bounds data isn't there for some reason...
-
Yes!
That did it! I can now achieve perfection.
Thank you for your time.
JerryC