Home > Mobile >  Change value based on another value dynamicly
Change value based on another value dynamicly

Time:01-31

Can't figure out math formula for changing one value based on another.

enter image description here I wanna do mechanic where player gets bigger when comes close to bottom border and shrincs when comes close to top. Red is Y coordinates (should not really matter but just to example) and green is desired player scale. So when player at the bottom of the screen he should be 1.25 scale and at the top 0.75. In course I am doing teacher uses this version (but this one doesnt work for me):

private void AdjustPerspective() {
        Vector3 scale = transform.localScale;
        scale.x = perspectiveScale * (scaleRatio - transform.position.y);
        scale.y = perspectiveScale * (scaleRatio - transform.position.y);
        transform.localScale = scale;
    }

First line just declare future scale variable. Second and third changing X and Y of scale (perspectiveScale and scaleRatio float variables which we created ourself for control and transform.position.y is player's Y position every frame) Fours line asigning new scale.
Can someone please help me with formula for this? Need to update scale value dynamicly depending on Y coordinate.

CodePudding user response:

Try this:

float scale = 1 perspectiveScale * (scaleRatio - transform.position.y);

In your case, with values like:

  • scaleRatio = 0
  • perspectiveScale = -0.25 / 4.2

And I recommend you to use intermediary variable, like this:

    Vector3 scale = transform.localScale;
    float newScale = 1   perspectiveScale * (scaleRatio - transform.position.y);
    scale.x = newScale;
    scale.y = newScale;
    transform.localScale = scale;

This way, you avoid error if you have to change your formula in the future.

Also, if nothing happens, don't forget to call your method in Update()

  • Related