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
2
Question by Diablo404 · Feb 14, 2014 at 08:49 AM · spriteunity4.3worldpos

Sprite scale and world scale

Hello everyone,

Searching for hours now. I basically want to scale a sprite ( Unity 4.3 ) by setting a world pos.

I'm searching for proportinal values but can't obtain something satisfying.

So basically I want something like gameObject.transform.localScale.x = 228 * ratio

Thanks in advance for any maths help :)

Edit:

Further explanations:

I have a simple Cube created in hierarchy which has a scale x and y of 45.

I have a sprite created from a texture ( 128 x 64 ) that I want to be exactly as big as the cube ( so 45 x 45 in world pos )

How can I, in code, calculate the ratio between the 100 x 100 values of the sprite scale in the inspector to the 45 x 45 worldspace pos that I want?

My sprites are in 100 pixelsToUnits.

My camera ortho size is half my screen height, so 500 actually.

Comment
Add comment · Show 2
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 robertbu · Feb 14, 2014 at 08:54 AM 0
Share

I'm puzzled by some things in your question. I don't know what you mean by 'scale a sprite by setting a world pos'. I also don't understand the 'ratio' you are looking for. ]

If you select your sprite texture in the Inspector, you will see a 'Pixels to Units' setting. This is the conversion factor for how big your sprite will be in world space when the sprite is scaled to (1,1,1).

avatar image Diablo404 · Feb 14, 2014 at 08:58 AM 0
Share

$$anonymous$$y bad for crappy description of my problem. Edited my original question for more informations

1 Reply

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

Answer by robertbu · Feb 14, 2014 at 09:38 AM

You can get the world size of your sprite from the Sprite.bounds. You can divide that size by your target size to get the size to set Transform.localScale. So if we wanted to set a sprite to be 45 units wide, we could do:

 #pragma strict
  
 function Start () {
     var bounds : Bounds = GetComponent(SpriteRenderer).sprite.bounds;
     var xSize = bounds.size.x;
     transform.localScale.x = 45.0 / xSize;    
 }
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 Diablo404 · Feb 14, 2014 at 09:58 AM 0
Share

Can't believe it was that simple... Well thank you so much you definitely made my day!

avatar image fish.soluble · Dec 01, 2014 at 08:04 AM 0
Share

The documentation for sprite.bounds (http://docs.unity3d.com/ScriptReference/Bounds.html) says it returns an axis-aligned box. That won't match the sprite's actual bounds if it's rotated. Furthermore, if the sprite is scaled, sprite.bounds doesn't appear to reflect that. Does anyone know an alternative that reports the sprite's bounds in non-axis-aligned local coordinates?

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

19 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

Related Questions

Sprite Rendering Cost Calculation 1 Answer

Background Sprite Setting 0 Answers

Apply shader to a sprite. 3 Answers

Jelly Object for 2D Game 2 Answers

how draw call,tris and verts work on unity? 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