- Home /
Unity 4.3 Sprite Align
I hope their is an easy answer to this. Below is the actual sprite sheet I'm using. I slice it automatically and drag all the sprites anto my scene to auto generate the animation. When I play the clip back the chimney appears to move upwards. I just want it to look like the chimney collapses an is laying down on the roof.
Maybe it's the sprites position within the keyframe? Or I need to auto align all the sprites to the bottom? I'm very new to unity and like everyone, very new to 4.3! Any help would be awesome.
Answer by Spinnernicholas · Nov 26, 2013 at 09:21 PM
The problem is being caused by the smoke, it pushes the bottom of the sprite down below the bottom of the building.
I quickly see two options:
Manually slice the sprites so that the bottom of each sprite is aligned to the bottom of the sprite where the smoke goes down the lowest. ie: if the smoke goes x pixels below the bottom of the chimney, then every sprite needs to have x pixels below the chimney.
Separate the chimney sprites from the effect sprites. You would then create effect sprites on top of the chimney sprites. You would only have to deal with aligning the effect sprites then.
I personally like option 2 because I think it is cleaner, but ultimately, they look the same.
Set the pivot point of each sprite in the slicer to the base of the chimney and then move the whole sprite down by the pivots y position at runtime.
void fixSpritePosition()
{
transform.position += new vector2(0,(SpriteRenderer)renderer.sprite.textureRectOffset * pixelToWorldConverter);
}
pixelToWorldConverter is a value that changes the pixel values to world space values. I'm not sure what this would be.
This will cause problems if you have a Rigidbody on the same GameObject as the SpriteRenderer. But, that's easy to fix, just move the Rigidbody to a new gameobject and place both objects under a parent object.
Answer by Key_Less · Nov 26, 2013 at 08:34 PM
If you can evenly space out your sprites on the sheet so that they all share equal amounts of area, then you should be able to slice them evenly and the animation will look correct.
I don't have any control over the sprites, is there definitely no way to do it with the image that I have?
That makes this an interesting issue, I can see exactly why the animation would seem to move up during its animation. I'm sure there is a way but I cannot be sure, I will need to do some research and get back to you. Going off nothing but pure speculation, I would start by parenting the Sprite to an empty GameObject. Then I would try to configure the center point for each sprite and use it as a sort of offset so that each image lines up as expected. Then during each frame of the animation, I would translate the sprite's position based of the difference in distance between the center point and the parent object. Again, I don't know if this is possible, just a brain storm.
Some pseudo code:
private function PositionSprite()
{
var centerPoint = sprite.rect.center;
// Get a vector between the parent object and center point.
var distanceBetweenPoints = gameObject.transform.parent.position;
// translate the sprite using the distance between points.
sprite.transform.Translate(distanceBetweenPoints);
}
Thanks for the brain storm (and answer) I don't have much time on this project so I'm going to take Spinnernicholas' quick fix for now. I'll try and let you now if your code solution works next week.