- Home /
How to use line renderer to create borders around an 2d object?
Hi,
I am using a 2D sprite and I want to create a dynamic border around the square using a Line Renderer component such that when I scale it should have its original border size as always on each scale event.
Any Psuedo code is appreciated.
Answer by hexagonius · Dec 08, 2016 at 06:54 PM
You can access a gameobjects sprites bounds, which are world space units, with:
GetComponent<SpriteRenderer>().sprite.bounds;
with the gameobject transforms left and right you get the world space coordinates for the bounds extends, e.g.:
Vector3 upperleft = transform.position - transform.right * bounds.extends.x + transform.up + bounds.extends.y)
and so on. Create 4 positions like this for the LineRenderer and assign them with:
GetComponent<LineRenderer>().SetPositions(positionsArray);
Your answer
Follow this Question
Related Questions
Line Renderer with sprites 0 Answers
Draw a line in local space of a GameObject 2 Answers
Scale down spritesheets without breaking anything else 0 Answers
How to tile sprite in 2D game when scaling instead of stretching 2 Answers
1px Gap Between 2D Sprites 2 Answers