Hello Guest

Author Topic: Best way to rotate sprites?  (Read 7059 times)

dpk

  • Newbie
  • *
  • Posts: 13
    • View Profile
Best way to rotate sprites?
« on: June 18, 2013, 05:06:23 am »
What's the best way to rotate a sprite with the 2D Toolkit? I am currently rotating by using Quaternions but I'm hoping there's a 2D Toolkit way to do a basic rotation without much fuss.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Best way to rotate sprites?
« Reply #1 on: June 18, 2013, 10:36:14 am »
transform.localEulerAngles = new Vector3( 0, 0, zrotation ); will do the trick?

dpk

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Best way to rotate sprites?
« Reply #2 on: June 18, 2013, 02:28:20 pm »
Unfortunately that doesn't seem to do it. The object gets rotated such that it is invisible to the player, perpendicular to the camera and rotated kind of weirdly otherwise. I think it's because the object itself has some rotation in order to move in a particular direction.

I should have mentioned this before but I am using the tk2d Camera for the scene. Is there a way to rotate the sprite as displayed in that camera without having to rotate the game object?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Best way to rotate sprites?
« Reply #3 on: June 18, 2013, 02:51:23 pm »
No, since the sprite is a component of the game object, you can't rotate it independently to the game object. You could make your sprite a child of the gameobject - that way you should be able to rotate that independently.

dpk

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Best way to rotate sprites?
« Reply #4 on: June 18, 2013, 04:01:49 pm »
OK. Thanks. I made the sprite a child of the gameobject. That seems to work OK but it means I have to manually set the box collider (in the parent object) or split out the rigidbody control and collider code in to separate scripts.

Finnegan

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 38
    • View Profile
Re: Best way to rotate sprites?
« Reply #5 on: August 20, 2013, 12:40:49 pm »
How can I rotate a batched sprite for some angle?

I'm trying
Code: [Select]
batchedSprite.rotation = Quaternion.AngleAxis(90, Vector3.forward); before building a batch but it doesn't work.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Best way to rotate sprites?
« Reply #6 on: August 20, 2013, 01:07:59 pm »
Quaternion.Euler( 0, 0, angle ) should work...

Finnegan

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 38
    • View Profile
Re: Best way to rotate sprites?
« Reply #7 on: August 20, 2013, 01:35:22 pm »
Thanks, but it didn't work either.  :(

Here's my full code:
Code: [Select]
tk2dStaticSpriteBatcher batcher = new GameObject().AddComponent<tk2dStaticSpriteBatcher>();
batcher.batchedSprites = new tk2dBatchedSprite[1];

tk2dBatchedSprite batchedSprite = new tk2dBatchedSprite();

batchedSprite.spriteCollection = spriteCollectionData;
batchedSprite.spriteId = spriteID;

Vector3 pos = new Vector3(100, 100, 0);
batchedSprite.relativeMatrix.SetTRS(pos, Quaternion.identity, Vector3.one);
batcher.batchedSprites[0] = batchedSprite;

//batchedSprite.rotation = Quaternion.AngleAxis(90, Vector3.forward);
batchedSprite.rotation = Quaternion.Euler(0, 0, 90);

batcher.SetFlag(tk2dStaticSpriteBatcher.Flags.GenerateCollider, false);
batcher.Build();
« Last Edit: August 20, 2013, 02:14:58 pm by Finnegan »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Best way to rotate sprites?
« Reply #8 on: August 20, 2013, 02:00:28 pm »
Thats not going to work - you need to set the rotation to SetTRS.
SetTRS(pos, Quaternion.Euler(0, 0, 90), Vector3.one));
You don't have to set it to .rotation