Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 kaiyum · Feb 20, 2014 at 06:29 PM · spriteposition

top right corner sprite position in screenspace(or worldspace)

Hi, I need to get the screenspace(or worldspace, it does not matter) position of top right corner of a sprite. How can this be done via script?

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

1 Reply

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

Answer by robertbu · Feb 20, 2014 at 08:49 PM

I could only find a pretty ugly solution. As with a lot of things dealing with Sprites, what should be easy is difficult or impossible. I'm hoping someone with more experience or knowledge of sprites can come up with something better.

The following code calculates the localTopRight in Start(). Then in Update() it places a small sphere at that location. If you are not going to be scaling or rotating your sprite, you can simplify this code greatly. Most of the complications are in dealing with rotation and scaling.

 #pragma strict
 
 private var go : GameObject;
 private var localTopRight : Vector3;
 
 function Start () {
 
     var q = transform.rotation;
     transform.rotation = Quaternion.identity;
     var bounds = GetComponent(SpriteRenderer).bounds;
     localTopRight = Vector3(bounds.extents.x, bounds.extents.y, 0.0) + transform.position;
     localTopRight = transform.InverseTransformPoint(localTopRight);
     transform.rotation = q;
     
     go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
     go.transform.localScale = Vector3(0.1, 0.1, 0.1);
 
 }
 
 function Update () {
     go.transform.position = transform.TransformPoint(localTopRight);
 }
Comment
Add comment · Show 3 · 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 kaiyum · Feb 21, 2014 at 01:59 PM 0
Share

@robertbu ,thanks for helping. can I go with 11 number's localTopRight in your code? Here I am converting it to local and then worldspace back again. Why do we do this? Is it for scale issues? Also can we not write like: transform.rotation=Quaternion.identity? In s$$anonymous$$d of storing it to temporary variable?

avatar image robertbu · Feb 21, 2014 at 03:37 PM 2
Share

Let me explain a bit about this code. What I want to get is the local corner so that if the object is rotated or scaled, I can find the world corner. SprinteRenderer.bounds is an axis aligned and in world coordinates. And when I start the script, I have no idea what angle the sprite will be. Imagine the sprite is rotated at 45 degrees,. The angle-aligned bounds will be much larger than if the rotation is 0.0.

So what I do is save the current rotation and then set the rotation to (0,0,0). This allows me to grab the world corner of the the unrotated object. But at a later time, the object may change scale or be rotated, so I convert the world coordinate into a local coordinate. This gives me a value I can use at some future time that will correctly find the corner no matter how the scale changes or how the rotation changes.

If your sprite is not scaled nor rotated, and you want the corner for this sprite, you can just do:

    var bounds = GetComponent(SpriteRenderer).bounds;
    var worldTopRight = Vector3(bounds.extents.x, bounds.extents.y, 0.0) + transform.position;
avatar image kaiyum · Feb 21, 2014 at 07:15 PM 0
Share

@robertbu, thank you very much. I really appreciate it. Now it looks clear to me.

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

18 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

Related Questions

How do I check if/where in a sprite another sprite is? 0 Answers

Change default sprite position when dragged into Hierarchy 1 Answer

How to fit rectangles to sprites? 1 Answer

Keep sprites in the same position relative to screen resolution. 2 Answers

Change Rotation Pivot without Affecting Position 1 Answer


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