Beispiel #1
0
    void OnTriggerEnter(Collider other)             // Applied on first hit. calculate knockback based on distance to center of explosion.       !!Can bug out in certain situations if player is already inside the trigger and thus never enters it. maybe timestep issue, object disappears before next cycle?!!
    //void OnTriggerStay(Collider other)          // knockback applied on every timestep as long as player is within radius, no need to calculate distance based force. Remember to apply damage just once.
    {
        // Explosion hits player
        if (other.tag == "Player")
        {
            /*  TRYING SPLITTING PROJECTILE DAMAGE TO IMPACT AND SPLASH DAMAGE INSTEAD OF DEALING FULL DAMAGE ON DIRECT HIT AND THEN IGNORING THAT TARGET WHEN CALCULATING SPLASH DAMAGE
             * // Ignore player that got hit directly by the projectile
             * if (directHit == other.gameObject)
             * {
             *  return;
             * }
             */

            // Calculate knockback
            knockback = (other.transform.position - transform.position).normalized * knockbackForce;           // Reduce knockback if further away?

            // Calculate splash damage
            //splashDamage =

            // Deal damage
            TargetDummy targetDummy = other.gameObject.GetComponent <TargetDummy>();     // Dummy for testing
            if (targetDummy != null)
            {
                // ... the enemy should take damage.    (projectile splash damage)
                targetDummy.TakeDamage(splashDamage, knockback);

                // Hp drops to 0 after taking damage
                if (targetDummy.currentHealth <= 0)
                {
                    //parentScript.PlayHitSounds(true);
                }
                else
                {
                    //parentScript.PlayHitSounds(false);
                }
            }

            PlayerHealth playerHealth = other.gameObject.GetComponent <PlayerHealth>();
            if (playerHealth != null)
            {
                // ... the enemy should take damage.
                playerHealth.TakeDamage(splashDamage, knockback);
                //playerHealth.TakeDamage(0, knockback);                      // 0 damage to players, for testin rocket jumps

                if (parentGameObject != other.transform.gameObject) // No hitsounds if self damage
                {
                    // Hp drops to 0 after taking damage
                    if (playerHealth.currentHealth <= 0)
                    {
                        //parentScript.PlayHitSounds(true);
                    }
                    else
                    {
                        //parentScript.PlayHitSounds(false);
                    }
                }
            }
        }
    }
Beispiel #2
0
    void HitDummy()
    {
        // Try and find an Health script on the gameobject hit.
        TargetDummy targetDummy = shootHit.collider.GetComponent <TargetDummy>();

        if (targetDummy != null)
        {
            // Knockback calculated using position of the weapon, use camera location instead to get correct vector?
            //knockback = transform.forward.normalized * knockbackForce;                                                    // Knockback direction (normalized) * force
            //knockback = (shootHit.transform.position - transform.position).normalized * knockbackForce;                   // Same as before

            knockback = camera.transform.forward.normalized * knockbackForce;

            // ... the enemy should take damage.
            targetDummy.TakeDamage(damagePerShot, knockback);

            // Hp drops to 0 after taking damage
            if (targetDummy.currentHealth <= 0)
            {
                PlayHitSounds(true);
            }
            else
            {
                PlayHitSounds(false);
            }
        }
    }
Beispiel #3
0
    void Fire()
    {
        // Reset the timer.
        timer = 0f;

        // Set the shootRay so that it starts at the end of the gun and points forward from the barrel.
        //shootRay.origin = transform.position;
        shootRay.origin    = camera.transform.position;     //fwd from camera
        shootRay.direction = transform.forward;

        // Perform the raycast against gameobjects on the shootable layer and if it hits something...
        if (Physics.Raycast(shootRay, out shootHit, range, shootableMask))
        {
            // Try and find an EnemyHealth script on the gameobject hit.
            //EnemyHealth enemyHealth = shootHit.collider.GetComponent<EnemyHealth>();
            TargetDummy targetDummy = shootHit.collider.GetComponent <TargetDummy>();

            // If the EnemyHealth component exist...
            if (targetDummy != null)
            {
                knockback = transform.forward * knockbackForce;

                // ... the enemy should take damage.
                targetDummy.TakeDamage(damagePerShot, knockback);
                PlayHitSounds();
            }

            //
            PlayerHealth playerHealth = shootHit.collider.GetComponent <PlayerHealth>();             //CLEAN UP LATER
            if (playerHealth != null)
            {
                knockback = transform.forward * knockbackForce;

                // ... the enemy should take damage.
                playerHealth.TakeDamage(damagePerShot, knockback);
                PlayHitSounds();
            }
            //

            // Impact effect
            Impact();
        }
        // If the raycast didn't hit anything on the shootable layer...
        else
        {
            // Endpoint = camera.fwd + max distance
        }
    }
    void Shoot()
    {
        isShooting = true;

        Debug.Log("PewPew");

        gunAudio.PlayOneShot(gunShoot);
        animator.SetTrigger("Shoot");
        muzzle.Play();

        shotsFired++;

        Vector3    tDirection = playerCam.transform.forward;
        RaycastHit hit;

        if (Physics.Raycast(playerCam.transform.position, tDirection, out hit))
        {
            hitObject = hit.transform.gameObject;

            Debug.Log(hitObject);

            if (hitObject.CompareTag("Enemy"))
            {
                killCount++;
                TargetDummy targetHP = hitObject.GetComponent <TargetDummy>();
                targetHP.TakeDamage();
            }
        }

        ammo--;

        isShooting = false;

        animator.SetTrigger("Shoot");

        accuracy = killCount / shotsFired;
    }
Beispiel #5
0
    //void FireHitScan()

    //[Command]
    void CmdFireHitScan()
    {
        // Reset the timer.
        timer = 0f;

        // Set start position of line renderer & enable it
        if (beam)
        {
            beamLine.enabled = true;
        }

        // Set the shootRay so that it starts at the end of the gun and points forward from the barrel.
        //shootRay.origin = transform.position;
        shootRay.origin    = camera.transform.position;     //fwd from camera
        shootRay.direction = transform.forward;

        // Perform the raycast against gameobjects on the shootable layer and if it hits something...
        if (Physics.Raycast(shootRay, out shootHit, range, shootableMask))
        {
            // Workaround for self hits. problem atleast with jumppads                                                              //<-- FIX. allow raycast to hit multiple targets + ignore shooter? + boolean for toggling multi target since only railgun hits mutiple targets
            if (transform.root.gameObject == shootHit.transform.gameObject)                                                         // Set layer locally? Start as enemy, change into player and then this new layer is ignored? should work as long as change isnt updated for other clients
            {                                                                                                                       // or just set starting point just outside of the players collider
                //return;
            }
            //

            // Try and find an Health script on the gameobject hit.
            TargetDummy targetDummy = shootHit.collider.GetComponent <TargetDummy>();

            if (targetDummy != null)
            {
                // Knockback calculated using position of the weapon, use camera location instead to get correct vector?
                //knockback = transform.forward.normalized * knockbackForce;                                                    // Knockback direction (normalized) * force
                //knockback = (shootHit.transform.position - transform.position).normalized * knockbackForce;                   // Same as before

                knockback = camera.transform.forward.normalized * knockbackForce;

                // ... the enemy should take damage.
                targetDummy.TakeDamage(damagePerShot, knockback);

                // Hp drops to 0 after taking damage
                if (targetDummy.currentHealth <= 0)
                {
                    PlayHitSounds(true);
                }
                else
                {
                    PlayHitSounds(false);
                }
            }

            // If the Health component exist...
            PlayerHealth playerHealth = shootHit.collider.GetComponent <PlayerHealth>();
            if (playerHealth != null)
            {
                knockback = camera.transform.forward.normalized * knockbackForce;

                // ... the enemy should take damage.
                playerHealth.TakeDamage(damagePerShot, knockback);

                // Hp drops to 0 after taking damage
                if (playerHealth.currentHealth <= 0)
                {
                    // Last hit
                    PlayHitSounds(true);
                }
                else
                {
                    PlayHitSounds(false);
                }
            }

            // Set the second position of the line renderer to the point the raycast hit.
            if (beam)
            {
                beamLine.SetPosition(1, shootHit.point);
            }

            // Impact effect
            Impact();
        }
        // If the raycast didn't hit anything on the shootable layer...
        else
        {
            // ... set the second position of the line renderer to the fullest extent of the gun's range.
            if (beam)
            {
                beamLine.SetPosition(1, shootRay.origin + shootRay.direction * range);
            }
        }
    }