How to make my Player smaller if he moves the y coordinate up?
Hey, im making a point and click game with 2d pictures. I want to make my player smaller when he goes up the picture (or y coordinate) to create a realistic feeling where it feels like the player is moving away from the camera.
im pretty new to scripting so i dont really have a clue.. but i thought about something like when the y coordinate of the player is increasing the y.scale should decrease but i dont know how to realize it?
Answer by tormentoarmagedoom · Feb 05, 2020 at 03:47 PM
Hello there.
Then you should create a function inside the update (so its exected every frame) wich controls the scale of the sprite.
You should find the relation between the y position and the scale. For start, you need to know the transform.position.Y coord (I supose is Y coord but if you rotated the camera can be any other comoponent) of your character. Lets say, the Y value can go from +50 (top of map) to -50 (botom of map). And when is at top you want the sprite to be 125% of its original size, and when its at bottom, at 75% of original size. Then assign a multiplier to the scale of the sprite, (Scale is a property of the Transform component of any object).
Some code like This:
void Update()
{
// Original Scale is always 1
// Scale is a vector, so you need to change all its components to mantain the aspect ratio)
int ScaleValue = 1-(TheSpriteObject.tranform.position.y/200);
TheCharacterObject.transform.scale = new Vector3 ( ScaleValue , ScaleValue , ScaleValue );
}
So each frame will be calculating the scale of the character.
This way, when position.y is -50 scale will be 1.25 (125%).
When position is +50, scale will be 0.75 (75%).
You need to find your way to get what you need. This is not about knowing commands or functions. Is about thinking.
Good Luck! Bye!"
thank you for your fast answer and your help! im getting it now, but i still have some questions: whats the difference between the SpriteObject and the CharacterObject? I mean my CharacterObject is my player but whats the SpriteObject? The Background? And if im trying to use the .scale it says theres only localScale, is this the same?
@tormentoarmagedoom So this is exactly what I've been looking for, but sadly I'm still a novice. I've been binging C# tutorials just so I can figure out how to add your script to a 2D game but I still can't wrap my head around how to do it. I understand where to add within the Update function but beyond that, I'm struggling with what fields I should be adding my size values to and what the language would be. My best guess is that "TheCharacterObject" would be the PlayerController for my project and theSpriteObject would then be the sprite used by that object but then that leaves me with errors in the console? Could I please bug you to elaborate further for a beginner?