- Home /
Highlight one cell on a GridObject (prefab included)
Hello Unity forums! I am trying to make a simple game, well kinda i guess. Anyways, i have a grid that i downloaded off of the internet, ill upload it in a sec, but my question you all of you people is, how am i able to highlight individual cells around their cell borders on this grid while holding down a key. The holding down a key parts easy, but im assuming i have to use ray cast? maybe? I'm unfamiliar with the topic. But any help with this would greatly be appreciated!
https://www.dropbox.com/s/1zb2r8pv1opf0au/BuildPlane.prefab
An example of what i want to do: (I drew the red box with paint, but this is the effect i would like to achieve.)
the image you uploaded makes it look like the cell is already highlighted with a red border. Is you're question how to only make this red border appear when a button is held down rather than a mouse over?
Trying to understand the question better.
@xandermacleod I drew that box around the cell with paint. $$anonymous$$y question is how to do that...
Answer by halukdemir · Dec 14, 2013 at 06:35 PM
You cannot draw cells directly. You should use game object prefab to place on grid as same size of cells. After that you can change their colors when on holding a key.
Ex: transform.renderer.material.color = Color.white;
You can store objects public positions on awaking.
Ex: tile.gridPositionX = 0; tile.gridPositionY = 0;
so you can easily compare these values in list.
Answer by xandermacleod · Dec 14, 2013 at 07:36 PM
Ok. Here's the approach I would take:
Create an array of empty gameobjects each of which should be positioned at the centre of each grid square. (Hence forth referred to as centreObjects).
Cast a ray from the mouse position to the ground, and store the hit location in a variable.
Compare the distance between the hit location and all of the centreObjects and find out which centreObject has the smallest distance to the hit location (distance can be found through Vector3 magnitude).
In advance, prepare a prefab with 4 particle systems that mimics the edges of a grid square.
Instantiate the prefab at the closest centreObject.
(Check each frame if the centreObject has changed, and if so, destroy the previous prefab, and instantiate a new one).
Take note, if your grid undulates then the approach to take is to have a flat grid of gameObjects high in the sky, then cast a ray downward and use the hit location for your centreObjects.
Well if i have maybe 2000 grid squares, is there any way to shorten the method. Wouldn't casting a ray and comparing the distances lag up the game?
It sure would at 2000+.
Another solution is to have box colliders on each centerObject. that way you can avoid the 2k+ for loop and just directly read out the centreObject associated with the hit.collider.
Your answer
Follow this Question
Related Questions
How to create a countdown counter based off of a Key press. 1 Answer
Ways to reduce draw cells 1 Answer
Inspector doesn't want to show keys in Scripts 2 Answers
Use the same input key 1 Answer
Switch from GUI button to toggle key 1 Answer