Beispiel #1
0
    void ShootWater()
    {
        //Debug.Log("Shoot");
        if (waterAmmoClip > 0)
        {
            Debug.Log("Shooting water!");
            waterAmmoClip--;

            RaycastHit hit = new RaycastHit();

            // getting camera transform for the raycast
            Transform camera = GameObject.FindGameObjectWithTag("HumanCamera").transform;

            // raycast
            if (Physics.Raycast(camera.position, camera.TransformDirection(Vector3.forward), out hit, waterRange))
            {
                Debug.Log("Raycasted object: " + hit.collider.name);
                // if the player shot flamable object that is on fire
                if (hit.collider.gameObject.tag == "Flamable")
                {
                    ItemScript itemScript = hit.collider.gameObject.GetComponent <ItemScript> ();

                    Debug.Log("Raycasted flamable object: " + hit.collider.name);

                    // used to show up UI elements, when the player points at fired object
                    if (itemScript.onFire)
                    {
                        // adding steam effect
                        itemScript.AddSteamEffect();

                        // passing the amount of water to itemscript
                        itemScript.amountOfWater += waterAmount;

                        Debug.Log("Amount Filled " + itemScript.amountOfWater);
                    }
                } // end of hit.tag = "Flamable"
            }     // end of Physics.Raycast
        }         // end of waterAmmoClip > 0
    }             // end of ShootWater()
    void OnCollisionEnter(Collision collision)
    {
        foreach (Collider col in Physics.OverlapSphere(this.transform.position, waterRadius))
        {
            // if the nearby object has flamable tag
            if (col.gameObject.tag == "Flamable")
            {
                // getting ItemScript script from the object nearby
                ItemScript itemScript = col.GetComponent <ItemScript> ();

                // if the object is on fire
                if (itemScript.onFire)
                {
                    // getting the closest face of the object that is near the water bomb
                    Vector3 closestPoint = col.GetComponent <Collider> ().ClosestPointOnBounds(this.gameObject.transform.position);
                    // distance between the closest face of the item on fire and the water bomb
                    float distance = Vector3.Distance(closestPoint, this.gameObject.transform.position);
                    // the closer the object the bigger the number which will lead to more water poured on the nearby objects on fire
                    float waterRate = waterRadius - distance;                     // example 10 - 3, waterRate = 7

                    // leting the item script handle extinguishing
                    itemScript.amountOfWater += (waterAmount * (waterRate / 10));

                    // adding steam particle effect
                    itemScript.AddSteamEffect();

                    //Debug.Log ("I'm a water bomb and I exploded on " + col.gameObject.name + " object, distance from the object is " + distance + ", amountfilled = " + itemScript.amountOfWater);
                } // end of !(itemScript.onFire)
            }     // end of (tag == "Flamable")
        }        // end of foreach

        // had issues with object being destroyed at the start because it collided with the human
        if (timeAlive > 0.2f)
        {
            Destroy(this.gameObject);
        }
    }    // end of OnCollisionEnter