# Brick Shooter (tutorial) question

This is with refence to the tutorial: http://unity3d.com/learn/tutorials/modules/beginner/physics/assignments/brick-shooter My question is once you are done breaking the bricks how would you score in this game? How do I script a code that can help me know if all the bricks are on the floor level (transform.y = 0) and keep a score accordingly? I am a total noob in Unity and would appreciate any help! thanks!

I tried to come up with a solution for this scenario, and I ended up with a new question.

I tried the following code in a script that I attached to the physics_powerCube prefab.
I thought this code would detect when individual physics_powerCube objects had been moved, either by being hit with the physics_samoflange, or by being pulled down by other cubes:

bool isTumbled = false;
public Vector3 startPos;
public Vector3 currentPos;

// Use this for initialization
void Start () {
startPos = transform.position;
}

// Update is called once per frame
void Update () {

if (!isTumbled)
{

currentPos = transform.position;

//Debug.Log(GetInstanceID() + "startPos = " + startPos.ToString());
//Debug.Log(GetInstanceID() + "currentPos = " + currentPos.ToString());

if (!(currentPos.Equals(startPos)))
{
isTumbled = true;
//Debug.Log(GetInstanceID() + “: isTumbled is true”);
//ToDo: Increase score.
}
}

}

But, guess what? The cubes settle after the Start() event has already been called. So currentPos.Equals(startPos) is never true in the Update() function. So, I tried something like this:

bool isTumbled = false;
public Vector3 startPos;
public Vector3 currentPos;

// Use this for initialization
void Start () {
startPos = transform.position;
}

// Update is called once per frame
void Update () {

if (!isTumbled)
{

currentPos = transform.position;

//Debug.Log(GetInstanceID() + "startPos = " + startPos.ToString());
//Debug.Log(GetInstanceID() + "currentPos = " + currentPos.ToString());

if ((Mathf.Abs(currentPos.x - startPos.x) > 0.3f)
(Mathf.Abs(currentPos.y - startPos.y) > 0.3f)
(Mathf.Abs(currentPos.z - startPos.z) > 0.3f))
{
isTumbled = true;
//Debug.Log(GetInstanceID() + “: isTumbled is true”);
//ToDo: Increase score.
}
}

}

I tried different values than 0.3f, but the results were not reliable. So I tried this:

bool isTumbled = false;

void OnCollisionEnter(Collision col)
{
if (!isTumbled)
{
if (!(col.gameObject.name.Contains(“powerCube”)))
{
Debug.Log(GetInstanceID() + ": has collided with " + col.gameObject.name.ToString());
isTumbled = true;
//ToDo: Increase score.
}
}
}

I used the if (!(col.gameObject.name.Contains(“powerCube”))) line because the settling issue was triggering the OnCollisionEnter() event, because the powerCubes were colliding with each other as they settled. Annoying!! I thought the if line would filter out collisions with other powerCubes, leaving only collisions with the samoflange and the floor_* objects. This produced a bit more reliable results.

But, wtf is with the settling? How do you prevent the settling? (That’s the new question I ended up with.) The meshes appear to be butted up right against each other. So I thought, ok, maybe the size of the box collider needs to be tweaked, but whether larger, smaller, or the same as the mesh, there was still settling. What am I missing? I think if the settling was eliminated, any of these chunks of code would be more reliable.

Here’s a video of the settling: http://youtu.be/CRKgdCNJPXU

Untested, but what about invisible trigger colliders with each block, detecting each block in OnTriggerStay(), then when a block leaves the trigger, OnTriggerExit(), it registers a score? That would eliminate all the position checking.

-Raiden

Bah! Perfect!

@raiden ,Please can you give a sample of what you mean?

@layinka

Sorry for getting back so late, but I’ve created a small project demonstrating how to do this.

I actually use OnCollisionEnter, and it works well. I added simple crosshairs to help visually know where your shooting the ball.

Let me know if you have any questions.

Enjoy

-Raiden