- Home /
Getting Sprite height/width in local space
I'm playing about in Unity2D trying to move two separate objects with the analog sticks.
When they go out of bounds of the camera - I want them to stop.
The sprites have a rigidbody2D attached (I found characterController had collisions between the two objects which I didn't want).
While I can get the objects to stop at the edge of the screen, half the sprite goes out of view as the position is measured from the centre of the sprite.
I'm having trouble preventing this however as the sprite width/height seems significantly larger than what's displayed on screen.
How should I be getting the actual size of the sprite?
Code below:
// Use this for initialization void Start () { rigidBody = GetComponent (); spriteRenderer = GetComponent ();
halfWidth = spriteRenderer.sprite.rect.width/2;
halfHeight = spriteRenderer.sprite.rect.height/2;
//Get the bottom left as a world co-ord.
screenBottomLeftInWorld = Camera.main.ScreenToWorldPoint(new Vector2(0,0));
//Get the top right as a world co-ord.
screenTopRightInWorld = Camera.main.ScreenToWorldPoint(new Vector2(Screen.width, Screen.height));
Debug.Log("bottomLeft " + screenBottomLeftInWorld.x + " , " + screenBottomLeftInWorld.y + "topRight " + screenTopRightInWorld.x + " , " + screenTopRightInWorld.y);
}
private float halfWidth;
private float halfHeight;
private Vector2 screenBottomLeftInWorld;
private Vector2 screenTopRightInWorld;
private SpriteRenderer spriteRenderer;
private Rigidbody2D rigidBody;
// Update is called once per frame
void Update ()
{
float xDirectionShipTwo = 0;
float yDirectionShipTwo = 0;
if(Input.GetButton("ShieldTwo"))
{
Debug.Log("Shield two active");
}
if(Input.GetAxisRaw("ShipTwoHorizontal") < 0)
{
xDirectionShipTwo -= speed;
}
if(Input.GetAxisRaw("ShipTwoHorizontal") > 0)
{
xDirectionShipTwo += speed;
}
if(Input.GetAxisRaw("ShipTwoVertical") > 0)
{
yDirectionShipTwo += speed;
}
if(Input.GetAxisRaw("ShipTwoVertical") < 0)
{
yDirectionShipTwo -= speed;
}
if(Input.GetAxisRaw("Fire2") > 0)
{
Debug.Log("Fire two hit");
}
float proposedX = rigidBody.position.x + xDirectionShipTwo;
float proposedY = rigidBody.position.y + yDirectionShipTwo;
if(proposedX < screenBottomLeftInWorld.x)
{
proposedX = screenBottomLeftInWorld.x + halfWidth;
Debug.Log("New x " + proposedX);
}
else if(proposedX > screenTopRightInWorld.x)
{
proposedX = screenTopRightInWorld.x - halfWidth;
Debug.Log("New x " + proposedX) ;
}
if(proposedY < screenBottomLeftInWorld.y)
{
proposedY = screenBottomLeftInWorld.y + halfHeight;
Debug.Log("New y " + proposedY);
}
else if(proposedY > screenTopRightInWorld.y)
{
proposedY = screenTopRightInWorld.y - halfHeight;
Debug.Log("New y " + proposedY);
}
Debug.Log(proposedX + ", " + proposedY);
rigidBody.MovePosition(new Vector2(proposedX, proposedY));
}
Answer by JoshuaMee · Apr 04, 2018 at 01:14 AM
Turns out I needed to be using :
halfWidth = spriteRenderer.bounds.size.x/2;
halfHeight = spriteRenderer.bounds.size.y/2;
Instead of:
halfWidth = spriteRenderer.sprite.rect.width/2;
halfHeight = spriteRenderer.sprite.rect.height/2;
The second seems to get the size of the sprite as you'd see it in Paint.net or the inspector of the sprite asset - the first seems to get the actual in game values you want to use.
Answer by losingisfun · Apr 04, 2018 at 12:59 AM
you can get that from the rect transform.
RectTransform thing;
float width = thing.rect.width;
EDIT: can also get it directly from a SpriteRenderer with
SpriteRenderer spriteRenderer;
float width = spriteRenderer.sprite.bounds.size.x;
Sorry for bringing this up but it the first Google result for "unity sprite width".
There's no RectTransform for world objects by default. And if you add one it won't be neither getting nor setting SpriteRenderer's size.you can get that from the rect transform.
Your answer

Follow this Question
Related Questions
Animated sprite's bounds not in sync with its visuals 0 Answers
Issue with checking to see whether the Bounds value's of two game objects equal each other 1 Answer
RigidBody2D Causing Sprites to Disappear on iOS device? 0 Answers
Rigidbody slowly floats out of collision box problem 0 Answers
sprite seams on edges, does it have to do with GetPadding 0 Answers