Exemplo n.º 1
0
 // Update is called once per frame
 //Update should check if the scene has been unpaused and if so count for 5 seconds whilst checking if the beam remains
 //within a small horizontal angle. If so, it should output some message to say so.
 void Update()
 {
     if (Time.timeScale != 0)
     {
         if (Quaternion.Angle(plank.rotation, Quaternion.identity) < delta_angle)
         {
             balance_time += Time.deltaTime;                  //add the frame time onto the balance time
             if (balance_time > delta_time && !levelComplete) //if equilibrium has been held for long enough then you have completed it
             {
                 balanced_text.enabled = true;                //reveal the BALANCED! text
                 MomentsDataScript.AddStreakPoint();
                 balance_streak_text.text = MomentsDataScript.GetStreakPoints().ToString("F0");
                 levelComplete            = true;
                 time_left_text.text      = "0.00";
             }
             else
             {
                 if (balance_time < delta_time)
                 {
                     time_left_text.text = (delta_time - balance_time).ToString("F2");
                 }
             }
         }
         else
         {
             balance_time = 0;       //if equilibrium is lost then reset the balance time
         }
     }
 }
Exemplo n.º 2
0
    // Use this for initialization
    void Awake()
    {
        float num_points = MomentsDataScript.GetStreakPoints();

        //testing
        //num_points = 6;
        balance_streak_text.text = num_points.ToString("F0");      //display the streak points

        levelComplete  = false;
        Time.timeScale = 0f;                       //freeze time for the setup of masses and forces before the unpause button is clicked
        float hinge_z = Random.Range(-0.5f, 0.5f); //random position along the plank length

        plank_position = plank.position;
        hinge.position = new Vector3(plank_position.x, plank_position.y, hinge_z * (plank.localScale.z));
        //plank.GetComponent<HingeJoint>().connectedBody = hinge.GetComponent<Rigidbody>();
        plank.GetComponent <HingeJoint>().anchor = new Vector3(0, 0, hinge_z);    //anchor position along the plank
        //set random mass of plank between 1 - 10kg.
        plank_mass           = plank.GetComponent <Rigidbody>().mass = Random.Range(1, 10);
        plank_mass_text.text = plank_mass.ToString("F0");

        num_boxes = 0;          //the number of boxes that should be added to the simulation
        //create the positions and masses for the boxes that will be added
        //get a random position along the plank for a box to start
        box_pos[0]  = Random.Range(-plank.transform.localScale.z / 2, plank.transform.localScale.z / 2);
        box_mass[0] = Random.Range(1, 10);       //random range for the box between 1 and 10
        num_boxes   = 1;

        //if the user has fewer that 3 points, only produce 1 box on start
        if (num_points < 3)
        {
            //get a random position along the plank for a box to start
            box_pos[0]  = Random.Range(-plank.transform.localScale.z / 2, plank.transform.localScale.z / 2);
            box_mass[0] = Random.Range(1, 10);       //random range for the box between 1 and 10
            num_boxes   = 1;
        }

        else if (num_points >= 3 && num_points < 5)
        {
            //get a random position along the plank for a box to start
            box_pos[0]  = Random.Range(-plank.transform.localScale.z / 2, plank.transform.localScale.z / 2);
            box_mass[0] = Random.Range(1, 10);       //random range for the box between 1 and 10
            box_pos[1]  = Random.Range(-plank.transform.localScale.z / 2, plank.transform.localScale.z / 2);
            box_mass[1] = Random.Range(1, 10);       //random range for the box between 1 and 10
            num_boxes   = 2;
        }
        else
        {
            //get a random position along the plank for a box to start
            box_pos[0]  = Random.Range(-plank.transform.localScale.z / 2, plank.transform.localScale.z / 2);
            box_mass[0] = Random.Range(1, 10);       //random range for the box between 1 and 10
            box_pos[1]  = Random.Range(-plank.transform.localScale.z / 2, plank.transform.localScale.z / 2);
            box_mass[1] = Random.Range(1, 10);       //random range for the box between 1 and 10
            box_pos[2]  = Random.Range(-plank.transform.localScale.z / 2, plank.transform.localScale.z / 2);
            box_mass[2] = Random.Range(1, 10);       //random range for the box between 1 and 10
            num_boxes   = 3;
        }
        //randomly position boxes on the plank
        CreateMasses();


        //listeners
        reset_button.onClick.AddListener(delegate { ResetScene(); });
        start_button.onClick.AddListener(delegate { StartTiming(); });
    }