Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by JoshuaMee · Apr 03, 2018 at 11:04 PM · rigidbody2dspritesbounds

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));
 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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;
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ThisIsDangerous · Sep 17, 2020 at 02:47 PM 0
Share

Sorry for bringing this up but it the first Google result for "unity sprite width".

you can get that from the rect transform.

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.
avatar image krupps · Dec 22, 2020 at 06:09 PM 0
Share

There's no width property

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

83 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges