Simplest way of detecting if there's an object at position X

Hi guys.

So I’m making a tetris-like game, and right now I have something that Instantiates blocks into a GameObject, and the GameObject moves in a direction, one unit at a time, every x time period.

Now I want to have each of those blocks to check ahead of themselves when they are supposed to move, and if there’s a block there with tag “block_stopped”, it will tag all the blocks in the object (there are always four) with “block_stopped”, and break the GameObject up.

Each block object has a SpriteRenderer and BoxCollider2D.

There are four Block GameObjects inside of an empty GameObject called p1DroppingController, and the script that controls the dropping is called p1DroppingScript.

I used a function but this function can’t be called… CheckPosForWall doesn’t autocomplete and mono doesn’t recognise that it’s there at all. Why?!

void Update () {
	if (dropCount <= 1f) {
		dropCount += Time.deltaTime;
	} else {
		dropCount -= 1f;
		// check collision
		foreach (Transform genericBlock in transform) {
			if (CheckPosForWall(new Vector3(transform.position.x, transform.position.y + 1f, transform.position.z))) {
			}
		}
		} else {
			transform.position = new Vector3 (transform.position.x, transform.position.y+1f, transform.position.z);
		}
	}
}

bool CheckPosForWall (Vector3 where) {
	Ray ray = Camera.main.ScreenPointToRay (where);
	Debug.DrawLine(Camera.main.transform.position, Camera.main.ScreenToWorldPoint(where));
	RaycastHit2D hit = Physics2D.GetRayIntersection (ray, Mathf.Infinity);
	if (hit.collider != null) {
		if (hit.collider.transform.tag == "block_stopped") {
			Debug.Log('y');
			return true;
		} else
			Debug.Log('n');
			return false;
	} else
		return false;
}

Thanks guy!!!

Standard trick is think of it as a grid. Every item is either in/claiming one grid space, or is moving into another. While moving to a new space, it claims both (or maybe 4, it it moves diagonal,) then gives up the old space when it reaches the new. No rayCasting at all. In games where units can’t freely overlap, you can often see this (like the original WarCraft.)

Setup maybe like int[,] Board = new int[20,20];. Where -1 means free, otherwise the # of the block in it. A better one might be to make a small class for each space, with maybe busy/free, the Transform, the big block, maybe which part of the big block… .

Of course, the actual move involves converting board spaces to world coords. But all the “thinking” is done in grid numbers. Like it moves from x=2 to 2.1, 2.2 … 3. The picture in game might really be moving from world 50 to world 55.

For groups of blocks, like a certain Russian block game, would add a rule that if the space you want to go into is occupied, but is part of the same big block, it’s fine. Then make sure you free/release in the correct order (the behind block claims a space, but the ahead block frees it!!)