# Legacy GMHelp! Figuring out math formula

G

#### Gabriel

##### Guest
So, I'm kind of having a math trouble to create a simple effect (let's call it so) in my game.

When the player is wearing this x-ray specs, I want a hidden arrow to show up, indicating the path to a secret area.

There are two conditions, though:
- The player can only see the arrow if he's close to it, therefore, the arrow opacity must change as he gets closer or farther.
- When the distance player - - - arrow is equal or less than 200 (pixels), the arrow must be completely opaque.

For that, I've used the following code in the arrow's draw event:
Code:
``````///Draw Self According to Ghost Distance
var op = 0;

if ghost.xray == true
{
if distance_to_object(ghost) <= 200
{
var op = 1;
}
}

draw_sprite_ext(sprite_index, 0, x, y, 1, 1, 0, c_white, op);``````
But I don't want the arrow to appear out of nowhere. Instead, it should appear gradually as the player approaches the point which is 200px farther from the arrow.

How can I add an else statement for that to happen? I can't quite figure which formula to use, so the opacity lowers as the player departs...

Last edited by a moderator:

#### FrostyCat

##### Member
Another common approach:
Code:
``````var dist = distance_to_object(ghost);
if (dist <= 200) {
op = 1;
} else if (dist <= 600) {
op = 1 - (dist - 200) / 400;
} else {
op = 0;
}``````
I suggest that you read the Wikipedia article on linear interpolation. It shows up so often in game development that there is no excuse not to learn it.

Edit: Fixed reversed interpolation.

Last edited:

#### sp202

##### Member
You could replace the if statements with a clamp, no idea whether it's technically better but it sure looks nicer.
Code:
``````min_dist=200
range=400

dist=distance_to_object(ghost)
op=clamp(1-(dist-min_dist)/range,0,1)``````

Last edited:
G

#### Gabriel

##### Guest
@Alisa, @FrostyCat Thank you so much!
@sp202 With clamp it doesn't work as I want to, because it does the opposite (the opacity will fade as the ghost gets closer).

Now I can have x-ray masters on my game.

#### sp202

##### Member
@Gabriel That was my mistake, I forgot to subtract from 1. I've edited the code, it should work now.

G

#### Gabriel

##### Guest
@Gabriel That was my mistake, I forgot to subtract from 1. I've edited the code, it should work now.
Perfect! Thanks