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 /
avatar image
0
Question by i_am_the_od · Jun 21, 2019 at 09:34 AM · spriterendererscalingbestpractices

Is it correct way to draw sprite between two points?,Is it the best way to draw prefab between 2 points?

Hello! I want to draw sprite between two points in 2d space. I have prefab with small scaleY value. So I just draw my prefab in the center between two points and then change scaleX value in order to fill all space between two points. My code is:

    //draw obstacle between two points
     Vector3 center = Vector3.Lerp(from, to, 0.5F);
     var direction = to - from;
     var angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
     var obj = Instantiate(obstacleObject, center, Quaternion.AngleAxis(angle, Vector3.forward));
     
     //scale it between two points
     var boundsSize = obj.GetComponent<SpriteRenderer>().bounds.size;
     var scaleXKoef = Vector2.Distance(from, to) / Math.Max(boundsSize.x, boundsSize.y);
     var localScale = obj.transform.localScale;
     obj.transform.localScale = new Vector3(localScale.x * scaleXKoef, localScale.y,localScale.z);

Is it the best way to solve my issue? I'm not particularly sure about the part where I get Math.Max between boundSize.x and boundSize.y. Looks pretty tricky.

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 metalted · Jun 21, 2019 at 03:26 PM 1
Share

As this is not really an answer to the question, so ill make it a comment. Simply said, if this code works for you, it is good. In my opinion, you handle the rotation of the object and the positioning very well. $$anonymous$$aybe if someone else would write the same script it would look very different. But no one is the same. So I think it is good, there is no weird code to make it work, it is straight forward. Only thing I would change is don't use var, but the actual type, but that's just a personal style. When your scripts keep growing bigger and bigger at some point you don't know what var actually means. Learning to write the correct type will help you a lot in the future.

avatar image i_am_the_od metalted · Jun 23, 2019 at 10:00 AM 0
Share

Thanks for your comment!

0 Replies

· Add your reply
  • Sort: 

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

108 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 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

Scaling GameObjects by Screen Resolution 0 Answers

Sprite renderer scaling 0 Answers

Replacing and stacking shaders for some objects 1 Answer

Best practices - Model, animation and material naming 3 Answers

Best practice to store levels and highscores etc 2 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