- Home /
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
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.
}
}
}
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!
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 :)
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;
Your answer
Follow this Question
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