コード例 #1
0
 void SpeedBoostOn(Powerup powerup)
 {
     // Player cannot stack speed boosts
     if (Powerup.GetSpeedBoost() != true)
     {
         Powerup.SetSpeedBoost(true);
         globalTime = Time.time;
         speed     *= 2;
         fireRate  /= 2;
         AudioSource.PlayClipAtPoint(speedBoostOn, transform.position);
     }
     powerup.Collected();
 }
コード例 #2
0
    void Update()
    {
        // Restrict player to game space
        float newx = Mathf.Clamp(transform.position.x, xmin, xmax);

        transform.position = new Vector3(newx, transform.position.y, transform.position.z);

        // Move left and right
        if (Input.GetKey(KeyCode.LeftArrow))
        {
            transform.position += Vector3.left * speed * Time.deltaTime;
        }
        else if (Input.GetKey(KeyCode.RightArrow))
        {
            transform.position += Vector3.right * speed * Time.deltaTime;
        }

        // If space is held down, fire lasers at a set rate
        // "Fire" refers to the method above via string
        if (Input.GetKeyDown(KeyCode.Space))
        {
            InvokeRepeating("Fire", 0.000001f, fireRate);
        }

        // If space is released, cease firing lasers
        if (Input.GetKeyUp(KeyCode.Space))
        {
            CancelInvoke("Fire");
        }

        // If speed boost expired, set speed and fire rate back to normal
        if (Powerup.GetSpeedBoost() && Time.time - globalTime >= 20f)
        {
            SpeedBoostOff();
        }

        // Set position of shield to be the same as the player
        if (Powerup.GetShield())
        {
            shieldImage.transform.position = transform.position;
        }
    }