Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Your method of checking players distance from an event?

I was wondering-
Is it possible to activate the contents of an event while they player is say, 5 or less tiles away from an event?

In my survival game, there really isn't a purpose for the more resource Dependant walls, and I want it to increase your "Sanity" meter (Variable) when near them.

I was thinking of setting the players X and Y values to a variable, and then setting the events X and Y variables to another variable, and have it compare the two. My only problem is that I'm unsure of how it will be layed out, and I want to know how everybody else does it.
 
Your basic thinking is correct, and here's the formula you need:
Code:
d = (x2 - x1) + (y2 - y1) # returns distance in tiles between [x1, y1] and [x2, y2]
Note that this is for 4-way-movement only - you need the square root of the individual coordinates for 8-way, like so:
Code:
d = Math.sqrt((x2 - x1)^2 + (y2 - y1)^2) # returns distance in tiles between [x1, y1] and [x2, y2]
 
If you are looking to make something happen when you are near something, rather than count the number of steps it would take to walk to something, you should use the second formula for both HOWEVER, you will get some performance boost if you don't get the squareroot. If you are just checking if you are near something, you can simply use the square. For instance, if you want something to happen when you are within 5 squares, do if ((x1-x2)**2 + (y1-y2)**2) < 25
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top