- Home /
Grid system detect where player is
Hi guys,
I would like to have a very simple grid ontop of a terrain. the grid system only has to detect in which grid segment the player is and was.
my goal is to let a player search for items in a grid segment and if the player has already searched in a grid segment he cannot search it again. like in this game.
http://armorgames.com/play/5766/planet-noevo
I have found 50000 Tutorials for grid systems but none of them is what I want. I am not a good programmer so I am not able to rewrite that kind of code to my needs :(
Answer by Alec-Slayden · Feb 24, 2011 at 03:03 PM
How about this:
Set a null object to a far corner of your terrain, and put the player inside of this null object for local space (or alternatively have world zero at the corner of the terrain, and use global space)
With that setup, you can easily have a script grab the player position at any given time, and divide it by the grid size to determine what grid he's in.
Using a 2 dimensional array you could keep track of information for the grid square.
EDIT:
Here's a Javascript example of a method, set up for a 100 x 100 grid of 1.0 x 1.0 squares.
var PlayerTransform : Transform;
var gridSize_X : float = 1.0; //Row value var gridSize_Z : float = 1.0; // Column value var gridInfo = new String[100, 100];
private var currentRow : int; private var currentColumn : int;
function Update(){ // Here we do the math, and make sure to clamp it within the array's bounds
if (gridSize_X != 0) //no dividing by zero!
currentRow = Mathf.Clamp(PlayerTransform.localPosition.x / gridSize_X, 0, 99);
if (gridSize_Z != 0)
currentColumn = Mathf.Clamp(PlayerTransform.localPosition.z / gridSize_Z, 0, 99);
}
function Dig(FoundItem:String){
// If the square isn't null, we must have assigned a string to it already!
if (gridInfo[currentRow, currentColumn]){
Debug.Log("This place has already been dug.");
}
else{
gridInfo[currentRow, currentColumn] = FoundItem;
}
}
How you would call the dig function is up to you. Right now you'd pass a string into it with the name of what is found, but you could presumeably set up the code determining what is found in the dig function itself.
Your answer
![](https://koobas.hobune.stream/wayback/20220613110033im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Grid Based Movement System (pokemon like) 1 Answer
Where can I learn to make a tile system? 1 Answer
Power Generation? 2 Answers
Clicking on objects 2 Answers