Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Apr 14, 2016 at 04:18 PM by haskell33 for the following reason:

read comments.

avatar image
0
Question by haskell33 · Apr 14, 2016 at 01:44 PM · 2dboxcollider2dboxrectcenter

Question regarding Rect center

HI there, I'm making my own box colliders and while doing this a confusion regarding Rect center appeared.

Apparently, I can have a gameObject at, let's say (0,0), and a script inside this same gameObject instantiating a Rect struct with center as (10, 10).

The Rect will be drawn at (0,0) position, though.

The impression I got is that the center is "virtual" (in the real sense of the word, nothing to do with virtual classes), and if I use Contains or other methods, it will be as if the Rect is solely centered in (10, 10) instead of (0,0), is that right?

Or will it be in the two places in same time?

To avoid any confusions in my own project, I'm always placing the gameObject in the desired spot and doing Rect.center = gameObject.transform.position.

p.s: I think the documentation should have been more specific about this.

Comment
Add comment · Show 3
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 Immanuel-Scholz · Apr 14, 2016 at 01:59 PM 1
Share

Please clarify your question. Are you talking about the struct UnityEngine.Rect? If so, what has this to do with drawing anything? The struct is just 4 float values. So who draws what using what? Can you add the code that draws wrongly?

$$anonymous$$y gut feeling would be, that you copy somewhere the struct and loose your changes (structs are copied on assignment, not referenced). Or that you talking about something completely different here - maybe you meant RectTransform and anchoredPosition and deltaSize and stuff?

avatar image haskell33 Immanuel-Scholz · Apr 14, 2016 at 04:18 PM 0
Share

I've found out what was wrong... $$anonymous$$y drawing function was:

 void OnDrawGizmos()
 {
     Gizmos.color = Color.white;
     Gizmos.DrawCube (transform.position, new Vector2 (box.width, box.height));
 }

Of course this would always draw the Rect in the same position as gameObject. Replacing transform.position with box.center fixed the problem.

avatar image Polymo · Apr 14, 2016 at 04:16 PM 0
Share

Rects are only value containers with a few helper methods. $$anonymous$$aybe what you want is Bounds.Contains from either the renderer or the collider (which you get in the position of your object). But you can always set the rect center to your objects position to have the same effect when calling contains on global positions.

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

65 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

Related Questions

Push only 1 box around at a time (2D) 1 Answer

Rigidbody2d getting stuck when moving horizontally at random points! Can't figure it out :( 1 Answer

Make a GUI.Box with an outline AND a transparent center? 1 Answer

Using rect.contains to check if gameobject is inside in worldspace 1 Answer

How to render a colored 2D rectangle. 6 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