Hello,
I have a stack of cubes that are initiated on start up - the cube colours are selected randomly from an array of Materials (4 max).
I want to be able to have an array on every cube that lists if the cube next to it, is the same colour, and if the cube next to that is the same colour, etc.
When the cubes are initiated, they fire a raycast from every face to find the colours from the cubes that surround it. So I’m able to reference the colours that surround a cube.
But I also want to reference the same coloured cubes that surround neighbouring same coloured cubes. So if I select a cube at random, I can see a list of all the cubes that are the same colour as well as neighbouring cubes.
This script is attached to every cube that is initiated:
var FaceUp : Color;
var CubeUp : GameObject;
var FaceDown : Color;
var CubeDown : GameObject;
var FaceLeft : Color;
var CubeLeft : GameObject;
var FaceRight : Color;
var CubeRight : GameObject;
var FaceFront : Color;
var CubeFront : GameObject;
var FaceRear : Color;
var CubeRear : GameObject;
var ThisColour : Color;
function Start(){
ThisColour = renderer.material.color;
while(true){
RaycastFire();
yield WaitForSeconds(0.5);
}
}
var showDebug : boolean = false;
function RaycastFire(){
/*
Fires raycast to only Down, Right and Rear. Becasue the neighbouring cubes will fire
a raycast at us, we might as well push the Up, Left and Forward colour to us too,
to save us from firing 3 extra rays - Thus, we get the cube colours that surround us
*/
var ray = transform.position;
var hitDown : RaycastHit;
var hitRight : RaycastHit;
var hitRear : RaycastHit;
if (Physics.Raycast (ray, -Vector3.up, hitDown, 100.0)) {
var hitColorDown = hitDown.collider.gameObject.renderer.material.color;
if(showDebug)
Debug.DrawLine (ray, hitDown.point, hitColorDown, 0.25);
FaceDown = hitColorDown;
CubeDown = hitDown.collider.gameObject;
hitDown.collider.gameObject.GetComponent(JengaBlock).ColourAbove(renderer.material.color, gameObject);
}
if (Physics.Raycast (ray, -Vector3.left, hitRight, 100.0)) {
var hitColorRight = hitRight.collider.gameObject.renderer.material.color;
if(showDebug)
Debug.DrawLine (ray, hitRight.point, hitColorRight, 0.25);
FaceRight = hitColorRight;
CubeRight = hitRight.collider.gameObject;
hitRight.collider.gameObject.GetComponent(JengaBlock).ColourLeft(renderer.material.color, gameObject);
}
if (Physics.Raycast (ray, -Vector3.forward, hitRear, 100.0)) {
var hitColorRear = hitRear.collider.gameObject.renderer.material.color;
if(showDebug)
Debug.DrawLine (ray, hitRear.point, hitColorRear, 0.25);
FaceRear = hitColorRight;
CubeRear = hitRear.collider.gameObject;
hitRear.collider.gameObject.GetComponent(JengaBlock).ColourFront(renderer.material.color, gameObject);
}
CheckForSame();
}
function ColourAbove(above : Color, obj : GameObject){
FaceUp = above;
CubeUp = obj;
}
function ColourLeft(left : Color, obj : GameObject){
FaceLeft = left;
CubeLeft = obj;
}
function ColourFront(front : Color, obj : GameObject){
FaceFront = front;
CubeFront = obj;
}
So now I have referenced the cubes around us, I now need to check if any of these are the same colour, if they’re not - then we don’t need to worry.
So I understand that I can do something like this:
if(FaceUp == ThisColour)
//The cube above us is the same colour
But now I want to check if that ‘FaceUp’ cube has any neighbours that are the same colour too (except us, as we already know we are the same colour).
Example:
Okay, so above is a 2D example of what I’m trying to achieve. The square 0 is the cube I have clicked on, I am able to find the squares next to it that are the same (1 and 2) by using ‘if(FaceUp == ThisColour)’ etc.
What I want to do now, is then ‘search’ squares 1 and 2, and see if there are any next to them. As you can see, square 2 has one square next to it (excluding square 0), so I want to add this square to the array. And so on.