Part 5: Block Collisions

Christiane Snyder
8/8/2016

At this point, the project actually has all the code we need it needs to react to gazing information! But the game itself needs some tweaking still. At the moment, you can still move sideways through stationary blocks. The following block can sense if its too close to another object to move towards it by including the following improvements in “Block”:

public void MoveLeft(){

    if (Downward) {

       bool LeftEdge = false;

       foreach (Transform child in transform) {

           if (Mathf.Round (child.position.x) == -2.0f) {

               LeftEdge = true;

               break;

           }

           RaycastHit hit;

           if (Physics.Raycast (child.position, new Vector3 (-1.0f, 0f, 0f), out hit)) {

               if (hit.distance < 1.0f && !hit.collider.transform.parent.Equals (child.parent)) {

                   LeftEdge = true;

                   break;

               }

           }

     }

     if (!LeftEdge) {

         transform.position = new Vector3 (Mathf.Round (transform.position.x - 1.0f), transform.position.y, 6.0f);

         UpdateCenter();

     }

   }

}

 

public void MoveRight(){

    if (Downward) {

       bool RightEdge = false;

       foreach (Transform child in transform) {

           if (Mathf.Round (child.position.x) == 12.0f) {

               RightEdge = true;

               break;

           }

           RaycastHit hit;

           if (Physics.Raycast (child.position, new Vector3 (1.0f, 0f, 0f), out hit)) {

               if (hit.distance < 1.0f && !hit.collider.transform.parent.Equals (child.parent)) {

                   RightEdge = true;

                   break;

               }

            }

       }

           

         if (!RightEdge) {

             transform.position = new Vector3 (Mathf.Round (transform.position.x + 1.0f), transform.position.y, 6.0f);

             UpdateCenter();

        }

    }

}

 

public void MoveFaster(){

     if (Downward) {

        Movement = new Vector3 (0f, 0.2f, 0f);

     }

}

 

public void Rotate(){

    if (Downward) {

       transform.rotation = transform.rotation * Quaternion.Euler (0f, 0f, 90f);

       transform.position = new Vector3 (Mathf.Round (transform.position.x), transform.position.y, 6.0f);

       UpdateCenter();

}

}