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 Spider_newgent · Jan 10, 2017 at 10:29 AM · 2d gamescalingperspective

How to change objects scale based on distance between 2 points

Hi all.

I'm making a 2d point and click adventure and am at the stage where my player is walking around scenes, but his scale never changes. I'm trying to figure out the best way to implement scaling, based on the characters Y position between the bottom of the walkable area, (foreground) and the horizon.

In order to simulate fake perspective/3d I need the scaling to be non-uniform, so he scales down faster as he approaches the horizon.

I'll code it myself, I'm just after some ideas on approaches to the problem and searching the forums and online turns up surprisingly few results.

If I've put this in the wrong place or anything, please let me know.

Thanks

Dan

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

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Spider_newgent · Jan 12, 2017 at 10:10 AM

Hi all and thanks for the responses.

My setup is that each scene has a prefab called ScaleManager. This has 2 children, one for the horizon point and one for the foreground point. The ScaleManager also holds the below script.

The idea is for the player to lerp his localScale between minScale and maxScale, depending on his position between the horizon and foreground. So, if horizon.y = 0, foreground.y = -4 and player.y = -2, percent should = 0.5f.

However, This doesn't work and gives some funky results. The percentage is all over the place and the final scale is off. Can anyone see anything wrong with my approach or script below?

 using UnityEngine;
 using System.Collections;
 
 public class ScaleManager : MonoBehaviour {
 
     GameObject Player;
     float currentY;
     float oldY;
     public float distance;
     public float percent;
     public float scale;
     float horizonPointY;
     float foregroundPointY;
     public Vector3 minScale;
     public Vector3 maxScale;
 
     void Start () {
         Player = GameObject.FindGameObjectWithTag("Player");
         horizonPointY = this.gameObject.transform.GetChild(0).transform.position.y;         // Y position of the horizon object
         foregroundPointY =  this.gameObject.transform.GetChild(1).transform.position.y;     // Y position of foreground obj
         distance = Mathf.Abs(foregroundPointY - horizonPointY);
         Debug.Log("H point: " + horizonPointY + " F point: " + foregroundPointY);
     }
 
     void Update() {
         oldY = currentY;
         currentY = Player.transform.position.y;
         percent = Player.transform.position.y / distance;                            // percentage travelled between the 2 points
 
         if (currentY != oldY) {                                                      // If player has moved 
             Player.transform.localScale = Vector3.Lerp(minScale, maxScale, percent); // Lerp between minScale & maxScale as player.transform.position.y moves between horizonPointY & forgroundPointY;
             scale = Player.transform.localScale.y;                                   //used to see scale numerically in the inspector.
         }
     }
 }
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
avatar image
1

Answer by Moresco78 · Jul 29, 2021 at 12:14 PM

This is a really old post now but this is such a niche problem so I feel like it's worth the necro. If anyone is still trying to achieve this, you might try my approach. I'm also making a point and click game with linear perspective in 2D, so this applied. It's really simple.

I decided the range by using the eventsystem to get the ideal values to plugin, and put this in the update method in an IF statement, like so:

  if (playerTransform.position.y > -99f && playerTransform.position.x < 930f && playerTransform.position.x > -250f)  {               
             float realScalar = GetScaleInRange(-250, 930, 0.3f, 0.77f, playerTransform.position.x);
             playerScale.x = realScalar;
             playerScale.y = realScalar;
             playerTransform.localScale = playerScale;
 }




Range -250f to 930f is my x axis. Obviously if the player is outside of this area, there is a code block to use the default scale. The ranges 0.3f and 0.77f are the scale min and max values. The code for GetScaleInRange is this:

  public float GetScaleInRange(float minRangeA, float maxRangeA, float minRangeB, float maxRangeB, float scaleSourceXY)  {
         float scaleValue = Mathf.Lerp(minRangeB, maxRangeB, Mathf.InverseLerp(minRangeA, maxRangeA, scaleSourceXY));
         return scaleValue;
  }

Basically it starts with the InverseLerp using the player's transform.position.x in my example (scaleSourceXY could be a Y axis instead it could be whatever). This returns a percentage between the minRangeA and maxRangeA, and then directly feeds that into the Lerp which calls it. Then it finds wherever that percentage sits between my arbitrarily chosen clamped value of 0.3f to 0.77f. I hope that makes sense. Anyway, the result is a smooth scale on that walkable range, between the furthest distance, -250f at scale 0.3f, to the closest distance of 930f at a scale of 0.77f. Consequently, the rest of the picture's walkable areas work at 0.78f so that's the characters default scale, set in the ELSE statement:

 else
         {
             playerScale.x = 0.78f;
             playerScale.y = 0.78f;
             playerTransform.localScale = playerScale;
         }




The next bit would be cool to somehow use some kind of collider to determine the min/max A-range automatically, and then turn on scaling if the player is standing on it. That's for another day. Cheers!

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
avatar image
0

Answer by BinxJinder · Jan 10, 2017 at 06:23 PM

I would suggest directly taking the Y coordinates of the player character and comparing it against a Screen.height / 2 value. This way it would work adaptively with different resolutions, and the player object decreases in size as it approaches Screen.height / 2, and larger towards max size when it reaches Screen.height (bottom of screen). Hope this helps :)

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
avatar image
0

Answer by RobAnthem · Jan 10, 2017 at 07:37 PM

I imagine you would do something like this.

                 Vector3 horizonOffset = gameObject.transform.position;
                 horizonOffset.y = 20;
                 gameObject.transform.localScale = Vector3.Distance(gameObject.transform.position, horizonOffset) / 10 * 2;
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

68 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

Related Questions

What resolution to make retro pixel game to scale up to HD? 1 Answer

Proper way to dynamically place UI elements in cropped viewports? 0 Answers

Scale Sprites Smoothly - problem 0 Answers

Mobile 2d Game Resolution scalling 0 Answers

Auto Scaling Camera 2D 0 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