- Home /
How do I get the edge of an object from transform.position?
I wanted to slide a player along a rectangle by using transform.translate until it reaches the edge of said rectangle.
The problem is transform.position gives me a vector3 at the center of the transform not one side of it. Does anyone know how I can get the positional value for the edge of one side of a rectangular transform?
Answer by koray1396 · Oct 04, 2014 at 07:27 AM
if i understood right, you are using sprites and BoxCollider2D. Then you can get this information from collider center and size, say one edge would have an x value of, center.x + (size.x / 2).
@koray1396's approach will work, but both 'center' and 'size' need to be converted to world coordinates...and if the sprite is rotated, you will have to calculate a line for the edge, not a position.
Answer by slake_it · Oct 09, 2016 at 12:35 AM
I've made an asset to solve this problem
Move unity 2D objects with a single line of code without worrying about scale ,sprite pivot , collider size or offset.
Example:
place the player at the start of the level using a single line of code:
player.MoveBottomLeft ( ground.GetTopLeft () )
Supports:
Sprite Renderer
Box Collider 2D
Circle Collider 2D
Asset Store URL: http://u3d.as/wkF
Documentation: http://cyborgassets.weebly.com/object-edges-2d.html
Video Tutorial: https://www.youtube.com/watch?v=T6sFnIigDvA
Hope it helps you & saves your time