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
1
Question by Skyward_ · Apr 19, 2016 at 05:57 AM · distancetargetx-axisscaledown

How can i scale down a sprite based on distance from the target?

I've been trying to figure out how i can scale a sprite gameobject as it reaches the target, the target does not move (dead center) based on the distance from the target i need the sprite to scale down in the x axis as it reaches the target.

Need to scale it down based on distance because if i do apply speed on the sprite over time, i need the scaling to remain at the same rate.

Any help would be greatly appreciated, thanks!

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
0

Answer by Theinsanekiller · Apr 19, 2016 at 07:10 AM

The easiest way to do it is you will have to get the distance between the two game objects and you can achieve it using Vector2.Distance () function and then you will have to set the x value of transform equals to the distance.

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 Skyward_ · Apr 19, 2016 at 04:15 PM 0
Share

I understand how to achieve the distance between the two objects but i'm having a difficulties reducing the scale over time as it approaches the target.

Thanks

avatar image Theinsanekiller · Apr 20, 2016 at 07:09 AM 0
Share

As far as i can Understand your object is constantly moving towards a target scale should be the distance. So what you will have to do is you will have to constantly calculate distance and supply the value the value to the sprite's scale in update.

avatar image
0

Answer by Mazer83 · Sep 11, 2019 at 01:24 PM

  • Decide what your max size should be.

  • Decide what your min size should be.

  • Decide the distance at which the scaling should start.

The sprite will be at max size (100%) when the sprite is over a certain distance from the target.

The sprite will be at min size (let's say, 20%) when the sprite is under a certain distance. Then you just interpolate linearly between the two.

 scaleFactor = maxScale - minScale  // (1 - 0.2)
 
 if(dist < minDist) //minDist is the min distance needed to not be scaling
 {
     scale = (dist/minDist) * scaleFactor; 
 }
 

So here if you're 50% of the distance, you scale down by 40%, which is halfway between the max and min scale.

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Target distance would only update closer 1 Answer

How do I get an NPC to stop Following a player based on distance? 1 Answer

targeting multiple enemys 1 Answer

Vector3.Project, how to? 2 Answers

Why are Raycast showing 0 distance when using multiple Raycast? 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