(SOLVED)Could someone help me with this please?

D

Den

Guest
I'm trying to make a Nuclear Throne type movement (The player faces the mouse and switches the direction it's facing based on where the mouse is) but I can't figure out how to fix this thing that's happening.
The system works fine when not moving and when moving to the right but when the player moves to the left
it faces away from the mouse like it's inverted or something.
Anyone know what could be causing this?? it's the first time I have ever done this type of movement so I'm
kinda stuck.

Here's what I have:
Code:
// Get the axis
var xaxis = (right_key - left_key);
var yaxis = (down_key - up_key);

// Get direction
var dir = point_direction(0, 0, xaxis, yaxis);

// Get the length
if (xaxis == 0 and yaxis == 0) {
    len = 0;
} else {
    len = spd;
}

// Get the hspd and vspd
hspd = lengthdir_x(len, dir);
vspd = lengthdir_y(len, dir);

// Move
phy_position_x += hspd;
phy_position_y += vspd;


// Control the sprite
image_speed = .6;
if(length == 0) sprite_index = spr_idle;

//Face the mouse
look_at = point_direction(x,y,mouse_x,mouse_y);

//Flip the sprite
if(mouse_x > x) {
    image_xscale = 1;
}else {
    image_xscale = -1;
}

// Horizontal sprites
if(hspd > 0) {
    sprite_index = spr_goblinRun_right;
}else if(hspd < 0){
    sprite_index = spr_goblinRun_left;
}
 

Simon Gust

Member
Where is look_at also used? Can you show some more code?
Also, why are you using
Code:
phy_position_x += hspd;
phy_position_y += vspd;
Do you have physics enabled?

I think I know why. When you change phy_position_x it doesn't change x which you are refrencing.
 
Last edited:

NightFrost

Member
The problem comes from you both setting the image_xscale based on mouse position, AND separately setting left/right facing sprite based on movement direction. So when you walk left hspd < 0 and sprite is left-facing, while mouse on the right will set xscale to 1 (not flip), meaning sprite will face away from mouse. When the mouse moves on left half, xscale goes to -1 and flips the sprite to face right, again away from mouse. In short, set right-facing sprite at create and do not change it afterwards by hspd.

EDIT: you probably are meaning to use len instead of length here:
Code:
if(length == 0) sprite_index = spr_idle;
 
  • Like
Reactions: Den

Yal

šŸ§ *penguin noises*
GMC Elder
//Flip the sprite if(mouse_x > x) { image_xscale = 1; }else { image_xscale = -1; } // Horizontal sprites if(hspd > 0) { sprite_index = spr_goblinRun_right; }else if(hspd < 0){ sprite_index = spr_goblinRun_left; }
You have separate sprites for left and right AND flip the scale based on the mouse? Flipping twice will result in no flipping, so this is probably your problem.
 
  • Like
Reactions: Den

NightFrost

Member
Ok that makes sense, thank you :) Do you have any suggestions how I could do it?
First for clarity, remove spr_goblinRun_left and rename the other to just spr_goblinRun. Then add a line of code here so you switch between idle and walking sprites properly:
Code:
if(len == 0) sprite_index = spr_idle;
else sprite_index = spr_goblinRun; // <--- Add this
Then, delete the five lines of code under // Horizontal sprites that set the sprite according to hspd. Now it should work, as the sprite is simply x-flipped depending on mouse position.
 
  • Like
Reactions: Den
D

Den

Guest
Now it should work, as the sprite is simply x-flipped depending on mouse position.
Thanks man I understand now where I was going wrong, appreciate your help and explanation of it all :) thank you.
 
Top