- Home /
2D Tilebased game with blocks. Only allow to place in corners.
Hello everyone! Im trying to make a tilebased game much like the board game Blockus. I know there are many like it but im making it to learn unity better.
I'm trying to make the blocks only get placed so the corners are the only thing the can touch.
Not like this:
I have colliders on tht block itself and a seperate child gameobject with colliders on the corner so that its only allowed to be placed when triggering with the corner collider. But it still gets placed alongside the other block. I know i can just add another collider to prevent this, but its alot of work since there is about 20 different blocks.
Is there a way to script it so it only places in the corners?
Here is some of the placement script:
public static GameObject selectedTile;
private Vector2 mousePos ;
private LevelBoard levelBoard;
bool isColliding = false;
bool tileIsSelected = false;
bool hasBeenPlaced = false;
void Update(){
if(tileIsSelected == true && hasBeenPlaced == false){
mousePos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
transform.position = new Vector2 (Mathf.Round (mousePos.x), Mathf.Round (mousePos.y));
}
if(tileIsSelected == true)
{
SelectedTile ();
}
}
void OnMouseDown(){
tileIsSelected = true;
}
void OnTriggerEnter2D(Collider2D triggerEnter){
if (triggerEnter.gameObject.tag == "PlayerTile") {
isColliding = true;
Debug.Log ("Triggering with" + name)
}
}
void OnTriggerStay2D(Collider2D triggerStay){
if(triggerStay.gameObject.tag == "PlayerTile"){
isColliding = true;
if (tileIsSelected == true) {
ChangeColorRed ();
}
}
}
void OnTriggerExit2D(Collider2D triggerExit){
if (triggerExit.gameObject.tag == "PlayerTile"){
isColliding = false;
if (tileIsSelected == true) {
ChangeColorBack ();
}
}
}
void SelectedTile(){
if (tileIsSelected == true) {
if (Input.GetMouseButtonDown (1)) {
transform.Rotate (Vector3.forward, 90f);
}
if (Input.GetMouseButtonDown (0) && isColliding == false && LevelBoard.tileOnLevelBoard == true
&& LevelBorder.tileOnLevelBorder == false && AllowedColliders.allowedPlacement == true) {
SpriteRenderer[] renderer = GetComponentsInChildren <SpriteRenderer> ();
foreach(SpriteRenderer r in renderer){
r.color = new Vector4 (r.color.r, r.color.g, r.color.b, 255);
}
tileIsSelected = false;
hasBeenPlaced = true;
LevelBoard.tileOnLevelBoard = false;
}
}
}
Thanks for your time !
Use a two-dimensional array to keep track of the tile information. For example, you get use int[fieldWidth,fieldLength] where each element [x,y] is either a 0 or 1, and 0 means empty while 1 means block. Then you can easily deter$$anonymous$$e the neighbors of any given tile [x,y] by going [x+1,y] and so on and check if there's a block at this adjacent tile position.
Thank you for your answer, not quite sure how to set it up like that. I dont have much experience with arrays in general.
Well, I suggest getting acquainted with some basic collection types like arrays, lists, and dictionaries, since you will use these three a lot and they upon up many possibilities that would be cumbersome or impossible to do without. I also took quite a long time before I learned to use collections :)
Your answer
Follow this Question
Related Questions
Polygon Collider 2d doesnt work properly 3 Answers
Button above Collider is being ignored when clicked. 0 Answers
Use transform.position in FixedUpdate to ensure collision with objects which have Velocity 0 Answers
BoxCollider2D have an offset on collision,Collider2D has an collision offset? 0 Answers
Is there a way of casting Rays at the end of the collider of an object ? 0 Answers