2D Toolkit Forum

2D Toolkit => Support => Topic started by: dpk on June 18, 2013, 05:06:23 am

Title: Best way to rotate sprites?
Post by: dpk 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.
Title: Re: Best way to rotate sprites?
Post by: unikronsoftware on June 18, 2013, 10:36:14 am
transform.localEulerAngles = new Vector3( 0, 0, zrotation ); will do the trick?
Title: Re: Best way to rotate sprites?
Post by: dpk 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?
Title: Re: Best way to rotate sprites?
Post by: unikronsoftware 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.
Title: Re: Best way to rotate sprites?
Post by: dpk 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.
Title: Re: Best way to rotate sprites?
Post by: Finnegan 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.
Title: Re: Best way to rotate sprites?
Post by: unikronsoftware on August 20, 2013, 01:07:59 pm
Quaternion.Euler( 0, 0, angle ) should work...
Title: Re: Best way to rotate sprites?
Post by: Finnegan 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();
Title: Re: Best way to rotate sprites?
Post by: unikronsoftware 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