Hello Guest

Author Topic: Pivot/anchor variable doesn't exists  (Read 4862 times)

xraven13

  • Newbie
  • *
  • Posts: 6
    • View Profile
Pivot/anchor variable doesn't exists
« on: January 01, 2016, 11:13:23 pm »
Hi!

I have one problem.
I use pivot to change the center of rotation/scale, but Unity works in way that it affects also the position of object - which is a problem.

I want that x,y coordinates of object are always in the top left. To make this work I need to use the value of pivot and offset the position.

But it seems 2DToolkit doesn't have this variable.
Since this is critical and I need to finish this project asap, could you please explain me how I can tweak 2DToolkit to make this possible?
Thank you!

EDIT: in return of the favor I will also share the code here for the case if someone else will need it. Thanks again. :)
« Last Edit: January 02, 2016, 10:37:09 am by xraven13 »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Pivot/anchor variable doesn't exists
« Reply #1 on: January 02, 2016, 12:41:30 pm »
This isn't possible in Unity, but you can get similar behaviour by creating an empty game object as a parent, then positioning that where you want to, and position the sprite under it as a child. You rotate the sprite to get the desired rotation, but move the base object based on the origin.


xraven13

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Pivot/anchor variable doesn't exists
« Reply #2 on: January 02, 2016, 05:11:49 pm »
I actually have this code already working with Unity Sprites and Sprite Renderer, but I also want to able to use this when using 2DToolkit sprites.

This is the code:
width = GetComponent<SpriteRenderer> ().bounds.size.x;
height = GetComponent<SpriteRenderer> ().bounds.size.y;
origin.x = width - renderer.sprite.pivot.x;
origin.y = height - renderer.sprite.pivot.y;

public float x {
   get{ return (transform.position.x-origin.x); }
   set { transform.position = new Vector3 (value+origin.x, transform.position.y, transform.position.z); }
}
public float y {
   get { return (transform.position.y)-origin.y; }
   set { transform.position = new Vector3 (transform.position.x, value+origin.y, transform.position.z); }
}

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Pivot/anchor variable doesn't exists
« Reply #3 on: January 02, 2016, 08:17:26 pm »
Not sure you need to approach it that way...
If you do       

Code: [Select]
tk2dBaseSprite baseSprite = GetComponent<tk2dBaseSprite>();
Vector3 topLeft = new Vector3(baseSprite.GetUntrimmedBounds().min.x, baseSprite.GetUntrimmedBounds().max.y, 0);
transform.position = new Vector3(x, y, 0) - topLeft;

That will position it relative to the top left. You can easily modify that to do any corner you like. Since the pivot is 0, 0, 0 all you need is the bounds to work out what the corners will be relative to it.

xraven13

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Pivot/anchor variable doesn't exists
« Reply #4 on: January 04, 2016, 01:25:10 pm »
Thank you, this works!  8)  So basically this is the same as the pivot for Sprite, right. At least, values are the same.
When setting y value I am subtracting from screenHeight because I am using top left coordinate system. (maybe it will be useful to someone)
If you use normal coordinate system, just remove "screenHeight -" part.

   public float x {
      get{
         Vector3 topLeft = new Vector3(baseSprite.GetUntrimmedBounds().min.x, baseSprite.GetUntrimmedBounds().max.y,0);
         return (new Vector3(transform.position.x, transform.position.y, transform.position.z) + topLeft).x;
      }
      set {
         Vector3 topLeft = new Vector3(baseSprite.GetUntrimmedBounds().min.x, baseSprite.GetUntrimmedBounds().max.y,0);
         transform.position = new Vector3(value, y, transform.position.z) - topLeft;
        }
   }

   public float y {
      get {
         Vector3 topLeft = new Vector3(baseSprite.GetUntrimmedBounds().min.x, baseSprite.GetUntrimmedBounds().max.y,0);
         return screenHeight - (new Vector3(transform.position.x, transform.position.y, transform.position.z) + topLeft).y;
      }
      set {
         Vector3 topLeft = new Vector3(baseSprite.GetUntrimmedBounds().min.x, baseSprite.GetUntrimmedBounds().max.y,0);
         transform.position = new Vector3(x, screenHeight - value, transform.position.z) - topLeft;
         //transform.position = new Vector3 (transform.position.x, screenHeight-(value-offsetY+origin.y), transform.position.z);
      }
   }
« Last Edit: January 04, 2016, 01:27:35 pm by xraven13 »