Search Unity

How to find the distance between the player and the shoreline?

Discussion in 'Scripting' started by toreau, Mar 12, 2015.

  1. toreau

    toreau

    Joined:
    Feb 8, 2014
    Posts:
    204
    I have an island in my game, with water of course :), and I'm looking into a way of finding the distance from the player to the nearest shoreline.

    Setting up "fake" gameobjects representing the shoreline is not an option.

    Any hints?

    Thanks in advance!
     
  2. L-Tyrosine

    L-Tyrosine

    Joined:
    Apr 27, 2011
    Posts:
    305
    What I would do:

    At game start, calc a XZ grid over the terrain with a chosen number of divisions. Collect in a list each of these points that have y below water line and that have at least one (of the 8) neighbor point at dry land. These will be the shore points. To check the nearest just calc distance between the player and each one. Even better, pre-calculate the nearest shore point at all the land grid points and get the calculated distance from a rounded xz player position.

    Of course you will have problems if your terrain contains rivers (this algorithm will consider river shore as well).
     
    toreau likes this.