Ejemplo n.º 1
0
    void Update()
    {
        // Check if the player has pressed the fire button and if enough time has elapsed since they last fired
        if (Input.GetButtonDown("Fire1") && Time.time > nextFire)
        {
            // Update the time when our player can fire next
            nextFire = Time.time + fireRate;

            // Start our ShotEffect coroutine to turn our laser line on and off
            StartCoroutine(ShotEffect());

            // Create a vector at the center of our camera's viewport
            Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0.0f));

            // Declare a raycast hit to store information about what our raycast has hit
            RaycastHit hit;

            // Set the start position for our visual effect for our laser to the position of gunEnd
            laserLine.SetPosition(0, gunEnd.position);

            // Check if our raycast has hit anything
            if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out hit, weaponRange))
            {
                // Set the end position for our laser line
                laserLine.SetPosition(1, hit.point);

                // Get a reference to a health script attached to the collider we hit
                DamageableObject health = hit.collider.GetComponent <DamageableObject>();

                // If there was a health script attached
                if (health != null)
                {
                    // Call the damage function of that script, passing in our gunDamage variable
                    health.Damage(gunDamage, damageType);
                }
                else
                {
                    //Debug.Log("hit " + hit.collider.gameObject);
                }


                // Check if the object we hit has a rigidbody attached
                if (hit.rigidbody != null)
                {
                    // Add force to the rigidbody we hit, in the direction from which it was hit
                    hit.rigidbody.AddForce(-hit.normal * hitForce);
                }

                damageType.SpawnPrefabEffect(hit.point);
            }
            else
            {
                // If we did not hit anything, set the end of the line to a position directly in front of the camera at the distance of weaponRange
                laserLine.SetPosition(1, rayOrigin + (fpsCam.transform.forward * weaponRange));
            }
        }
    }
Ejemplo n.º 2
0
    // Giving damage to object if it`s have
    // DamageableObject component
    private void OnTriggerEnter2D(Collider2D col)
    {
        DamageableObject DamageObj = col.GetComponent <DamageableObject>();

        if (DamageObj != null && !_isAlreadyDamaged)
        {
            _isAlreadyDamaged = true;
            DamageObj.Damage(_damage);
            OnDeath();
        }
    }