# How to fake a 3d character displacement by sprite scaling in GameMaker?

I want my 2D sprite to become bigger when moving down the screen (closer to the camera), smaller when walking away and to maintain the same size when moving left or right.

The scaling along the y axis is fine but when I move left the sprite remains facing right. I have tied the y_scale to the x_scale so the sprite retains the correct proportions. Here is the problem:

For example, if the image_yscale is currently 9 and I want to move left, setting the image_xscale to -1 causes the sprite to shrink again. How can I flip the sprite along the x_scale while retaining the current size?

Here is my code:

// Sprite Scaling

if keyboard_check(vk_up)
{
image_yscale -= 0.01
}

if keyboard_check(vk_down)
{
image_yscale += 0.01
}

image_xscale = image_yscale

if image_yscale < 1
{
image_yscale = 1
}


Thanks for any help in advance.

• If you have some code that works partially, you should add it here (edit the question), this would probably help point you in the right direction! Sep 30, 2016 at 12:44
• How about the KISS principle? Why not just switch to a different sprite which is natively larger? Save in the cost of resize computations. Sep 30, 2016 at 16:08
• I need smooth scaling of the sprite size so it's not really appropriate for this, thanks anyway. Sep 30, 2016 at 16:21
• I have rolled back the question to a previous version. Please do not change the scope of the question content as it makes the answers no longer relevant. You can add how you fixed your issue as an answer, which is perfectly acceptable for this site :) A less preferred way to add the answer is to do an edit and clearly mark that's how you fixed your question but leave the question intact :) Sep 30, 2016 at 16:57
• Also, on modern hardware accelerated rendering scaling doesn't really cost much. Swapping textures might even cost more.
– user35344
Sep 30, 2016 at 16:58

So I think you want to change the line image_xscale = image_yscale to something like the following:

image_xscale = image_yscale

if keyboard_check(vk_left)
{
image_xscale *= -1
}


Basically simply setting the x_scale to -1 when the player is walking left won't cut it, because that shrinks the sprite down as you said. As negative scales are considered as flipping the sprite around, this code should do the trick. Basically we use the value from y_scale, but invert it if the player is walking left.

EDIT: To fix the sprite flipping back to facing right when the player releases the left arrow, you need to introduce a boolean value that stores the last direction key held. So change your code to something like this:

image_xscale = image_yscale

if keyboard_check(vk_left)
{
faceLeft = true
}
if keyboard_check(vk_right)
{
faceLeft = false
}

if faceLeft
{
image_xscale *= -1
}


Here faceLeft is a member value of your player object (or something similar, I'm not familiar with GameMaker).

• That is great and it works while moving the player to the left, but the sprite reverts back to facing right when I am not pressing any keys. Any idea how to fix that? Sep 30, 2016 at 15:34
• That would be fixed by introducing an additional boolean. I'll edit my answer.
– user35344
Sep 30, 2016 at 15:41
• Brilliant thank you! I have one more question if you are not bored yet, I will update the original post Sep 30, 2016 at 16:22
• Don't edit your question with the solution. If you have more trouble, you should post a new question, as this question has now been answered.
– user35344
Sep 30, 2016 at 16:56

In all honest, as much as i love GameMaker, i would not recommend this style of "2d" to be done using GameMaker.

But onto my approach to it!

After much thought, and some tinkering you probably want the character to gradually get bigger? Lets say yes.

So you have something in place for scaling? Good i don't have to re invent the wheel.

So we take your character, maybe use a Platformer physics Based on the broad question, im doing my best here So set up your platformer physics, and have them separate to the scaler. Now as for sprite fliping, your code is probably getting your scale_sprite; and move_object mixed up, so to fix this write a complete new method that does not affect the sprites size, and instead flip the sprite based on direction moved.

Use this;

image_index = 1;


Then every time your player pressed to move in a direction, flip it by using 1, or -1.

This is the way i would do it, but without knowing how you have implemented the scaling, i cant be 100% accurate with my answer.

I hope this helped, just throw a comment if you need any more help.

• Ok so this shouldn't affect sprite rotation, do you have any other code that touches or handles the sprite? Sep 30, 2016 at 15:01