2D Toolkit Forum
2D Toolkit => Support => Topic started 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.
-
transform.localEulerAngles = new Vector3( 0, 0, zrotation ); will do the trick?
-
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?
-
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.
-
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.
-
How can I rotate a batched sprite for some angle?
I'm trying batchedSprite.rotation = Quaternion.AngleAxis(90, Vector3.forward);
before building a batch but it doesn't work.
-
Quaternion.Euler( 0, 0, angle ) should work...
-
Thanks, but it didn't work either. :(
Here's my full code:
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();
-
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