Question by
TheNoobyBoy · Jan 07, 2018 at 08:22 PM ·
spritepositionunity 2dborder
How do I get the positions of the corners of a sprite?
I have the sprite of a rectangle and I'd like to get the positions of it's corners in the form of multiples vector2 for example.
Comment
Best Answer
Answer by xTop-SecreTx · Jan 07, 2018 at 09:54 PM
Subtract the half of width and height from it's coordinates
Answer by Dustin27 · May 17, 2021 at 09:24 AM
When you have also a rotation, this should do the job:
/// <summary>
/// Returns corners of a sprite in order [TopRight, TopLeft, BottomLeft, BottomRight]
/// </summary>
public static Vector3[] GetSpriteCorners(SpriteRenderer renderer)
{
Vector3 topRight = renderer.transform.TransformPoint(renderer.sprite.bounds.max);
Vector3 topLeft = renderer.transform.TransformPoint(new Vector3(renderer.sprite.bounds.max.x, renderer.sprite.bounds.min.y, 0));
Vector3 botLeft = renderer.transform.TransformPoint(renderer.sprite.bounds.min);
Vector3 botRight = renderer.transform.TransformPoint(new Vector3(renderer.sprite.bounds.min.x, renderer.sprite.bounds.max.y, 0));
return new Vector3[] { topRight, topLeft, botLeft, botRight };
}
Your answer
Follow this Question
Related Questions
localScale.x Affecting Player Translations 0 Answers
Problem with Sprite Editor 0 Answers
Cycling through array not working. Pulling my hair over this one. 0 Answers
creating a 2d peeling system in unity 0 Answers