• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Get mouse position in relation to character

A

AutoLiMax

Guest
Hi, I'm trying to flip the character left or right depending on the mouse position in relation to the character.

So if the mouse is on the right side of the character it would face right and if mouse is on the left side of the character it would face left.

Currently I have it set up to flip the character based on the mouse position on screen like so:
Code:
//Set character facing direction
var mouseX = window_mouse_get_x();
if (mouseX > 640)
{
    image_xscale = (1);
}
else
{
    image_xscale = (-1);
}
This works fine until the character gets to the edge of the level which messes it all up.

I need to say something like
Code:
if (mouseX position in relation to the character > 0)
{
image_xscale = (1);
}
else
{
image_xscale = (-1);
}
How would one go about doing this?

Thanks!
 
A

Aura

Guest
Using mouse_x should return x mouse position relative to the room and that's probably what you'd want to do. ^^
 
A

AutoLiMax

Guest
Thank you very much! that is exactly what I needed! one day I'll get the hang of it. :p

Thanks again
 
A

AutoLiMax

Guest
This brings me to the next problem which is finding the distance between the player and the mouse.
I've read about distance_to_object and distance_to_point but I'm not sure this will work as the "if distance_to_object(obj_Player) < range" is not being called from the obj_Player.

Basically I need to click an object and perform an action. But only if the object is within reach.
so:
Code:
if (distance between mouse or object and player is 16)
{
     blah blah;
}
Any ideas?

Thanks!

Never mind again. I set it to run from the player object and all is working fine.... for now....
 
Last edited by a moderator:

Mazey

Member
I like to use point_distance
Code:
point_distance(obj_Player.x,obj_Player.y,mouse_x,mouse_y) //returns a real number
distance_to_object is basically the same, but with less freedom (x1 and y1 are the x and y of the object you're calling it from, and x2 and y2 can only be an object)
 
Top