Example #1
0
    void Gust()
    {
        int spawnPoint = Random.Range(0, 4);

        spawnPoints[spawnPoint].GetComponent <ParticleSystem>().Play();

        float directionMod = (spawnPoint == 2 || spawnPoint == 3) ? -1f : 1f;

        _tower.AddXForce(Random.Range(minForce, maxForce) * directionMod);

        // Play gust sound.
        if (gustSound != null)
        {
            gustSound.Post(gameObject);
        }
    }
Example #2
0
    void Update()
    {
        bool left  = Input.GetKeyDown(fireLeft);
        bool right = Input.GetKeyDown(fireRight);

        if (left || right)
        {
            float directionMod = left ? 1f : -1f;

            // Sprite change.
            sprite.flipX = right;

            // Get next throwable.
            var thrownItem = _held;
            thrownItem.transform.SetParent(null);
            thrownItem.transform.position = left ? throwPointLeft.transform.position : throwPointRight.transform.position;
            thrownItem.transform.rotation = gameObject.transform.rotation;
            thrownItem.GetComponent <Collider2D>().enabled = true;

            var thrownRigidBody = thrownItem.GetComponent <Rigidbody2D>();
            thrownRigidBody.isKinematic = false;

            // Physics.
            float   objMass = thrownRigidBody.mass;
            float   thrust  = objMass * ACCELERATION;
            Vector2 angle   = Vector2.Lerp(minThrowAngle, maxThrowAngle, Random.value);
            thrownRigidBody.AddForce(new Vector2(angle.x * thrust * directionMod, angle.y * thrust));
            thrownRigidBody.AddTorque(Random.Range(-1f * throwTorque * thrust, throwTorque * thrust));
            _tower.AddXForce(directionMod * thrust * .1f);

            // Process throw.
            thrownItem.GetComponent <Throwable>().Throw();

            // Queue next items.
            LoadNextItem();
        }
    }