- Home /
Question by
Ekta-Mehta-D · Mar 23, 2013 at 11:18 AM ·
gridcellarrange
Update Hex Grid Cell After Shooting
i have arranged game object sphere in hex grid cell and some of the cell i have filled with empty transform.
When i shoot i am positioning my bullet sphere in empty position.. but next time when i shoot , my previous bullet disappears.
So how to arrange game object in grid so that it persist until i destroy.
Code to generate grid :
public var sphere : Transform[];
private var B : int = 0;
private var G : int = 1;
private var P : int = 2;
private var R : int = 3;
private var Y : int = 4;
private var E : int = 5;
public var m : int= 12;
public var n : int = 12;
public var radius : float = 0.5f;
public var useAsInnerCircleRadius : boolean = true;
private var go : GameObject;
private var offsetX : float;
private var offsetY : float;
private var formation = [ [E ,E ,E ,E, E, E, B, Y, Y ],
[E ,E ,E ,E, E, E, Y, Y, Y ],
[E ,E ,E ,E, E, E, Y, G, G ],
[E ,E ,E ,E, E, E, P, R, G ],
[E ,E ,E ,E, E, E, R, R, R ],
[E ,E ,E ,E, E, E, R, R, R ],
[E ,E ,E ,E, E, E, P, B, B ],
[E ,E ,E ,E, E, E, B, Y, B ],
[E ,E ,E ,E, E, E, G, E, Y ] ];
function Start() {
CreateGrid();
//StartAdjustments();
}
function HexOffset( x : int, y : int) : Vector2
{
var position : Vector2 = Vector2.zero;
if( y % 2 == 0 ) {
position.x = x ;//* offsetX;
position.y = y ;//* offsetY;
}
else {
position.x = ( x + 0.5f );// * offsetX;
position.y = y;// * offsetY;
}
return position;
}
function CreateGrid()
{
go = GameObject.Find("Grid");
var unitLengthv : float = ( useAsInnerCircleRadius )? (radius / (Mathf.Sqrt(3)/2)) : radius;
offsetX = Mathf.Sqrt(3);
offsetY = 1.5f;
for(var i : int = 0; i < formation.length; i++ )
{
for(var j : int = 0; j < formation[i].length; j++ )
{
var hexpos : Vector2 = HexOffset( i, j );
var pos : Vector3 = new Vector3( hexpos.x, hexpos.y, 0 ) + transform.position;
var tempsphere : Transform = Instantiate(sphere[formation[i][j]], pos, Quaternion.identity );
tempsphere.transform.parent = go.gameObject.transform;
if(formation[i][j] != E)
{
tempsphere.transform.rigidbody.constraints = RigidbodyConstraints.FreezeAll;
}
tempsphere.GetComponent(CellScript).position = pos;
tempsphere.GetComponent(CellScript).i = i;
tempsphere.GetComponent(CellScript).j = j;
}
}
}
code which i have tried :
function OnCollisionEnter(collision : Collision )
{
if(transform.parent == null)
{
var gs : Grid = GameObject.Find("Grid").GetComponent(Grid);
var hitColliders = Physics.OverlapSphere(collision.transform.position, 0.7);
if(bubbleId.Equals(collision.gameObject.tag))
{
for (var i : int = 0; i < hitColliders.Length; i++)
{
Destroy(hitColliders[i].gameObject);
gs.DestroySphere(hitColliders[i].gameObject.transform.GetComponent(CellScript).i ,hitColliders[i].gameObject.transform.GetComponent(CellScript).j , hitColliders[i].gameObject.transform.GetComponent(CellScript).position);
Destroy(gameObject);
}
}
else
{
var contact : ContactPoint = collision.contacts[0];
var pos : Vector3 = contact.point;
var t : Transform = ClosestTransform(pos);
transform.position = t.position;
transform.rotation = t.rotation;
transform.rigidbody.velocity = Vector3.zero;
transform.rigidbody.angularVelocity = Vector3.zero;
transform.rigidbody.isKinematic = true;
}
}
}
cell script :
public var position : Vector3;
public var color : String;
public var i : int;
public var j : int;
I am currently learning hex grid.. so pleaze guide me..
Thanks in advance for your support and help..
Comment
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Reduce draw call when drawing Grid! 0 Answers
How to name each grid cell in a grid... 0 Answers
GridLayout dynamic cell size based on object count 0 Answers