Esempio n. 1
0
 public override void Fire(Vector3 targetPoint)
 {
     if (CanFire() && gunnerPhotonView.IsMine)
     {
         int i = 0;
         foreach (Transform barrel in overrideBarrelTransforms)
         {
             targetPoint =
                 CalculateFireDeviation(targetPoint, projectileDeviationDegrees);
             currentCooldown = fireRate;
             UseAmmo(ammoPerShot);
             //     float distanceMultiplier = CalculateDamageMultiplierCurve(Vector3.Distance(barrelTransform.position, targetPoint));
             // define weapon damage details
             WeaponDamageDetails weaponDamageDetails = new WeaponDamageDetails(myNickName, myPlayerId, myTeamId,
                                                                               damageType, baseDamage, Vector3.zero);
             string weaponDamageDetailsJson = JsonUtility.ToJson(weaponDamageDetails);
             int    j = 0;
             if (i >= 2)
             {
                 j = 1;
             }
             StartCoroutine(delayedFire(j * 0.1f, targetPoint, weaponDamageDetailsJson, i));
             // do the rest in subclass
             i++;
         }
     }
 }
    // Start is called before the first frame update
    // ---------------------- COPY THESE FUNCTIONS FOR EACH CHILD CLASS --------------------------------//
    // ---------------------- RPCs DO NOT INHERIT FROM PARENT ------------------------------------------//

    // method called by weaponController
    // we need a new version of this for every child class, otherwise the top level RPC will be called
    public override void Fire(Vector3 targetPoint)
    {
        // Debug.LogWarning("Projectile Weapon has not been ported to the new PhysX system");
        //  return;

        if (CanFire() && gunnerPhotonView.IsMine)
        {
            targetPoint =
                CalculateFireDeviation(targetPoint, projectileDeviationDegrees);
            currentCooldown = fireRate;
            UseAmmo(ammoPerShot);
            //     float distanceMultiplier = CalculateDamageMultiplierCurve(Vector3.Distance(barrelTransform.position, targetPoint));
            // define weapon damage details
            WeaponDamageDetails weaponDamageDetails = new WeaponDamageDetails(myNickName, myPlayerId, myTeamId, damageType, baseDamage, Vector3.zero);
            string weaponDamageDetailsJson          = JsonUtility.ToJson(weaponDamageDetails);
            weaponPhotonView.RPC(nameof(FireRPC_ProjectileWeapon), RpcTarget.All, targetPoint, weaponDamageDetailsJson);
            ShakeCameras(cameraShakeAmplitude, cameraShakeDuration);
            // do the rest in subclass
        }
    }
Esempio n. 3
0
    public override void Fire(Vector3 targetPoint)
    {
        /*if (gunnerPhotonView.IsMine && !isRemotelyFiring && HasAmmoToShoot())
         * {
         *
         * }*/

        /*
         * if (!CanFire() && gunnerPhotonView.IsMine)
         * {
         *  SetIsRemotelyFiring(false);
         *  CeaseFire();
         * }*/



        if (base.CanFire() && gunnerPhotonView.IsMine && CanFire())
        {
            timeSinceLastFire = 0;
            targetPoint       =
                CalculateFireDeviation(targetPoint, projectileDeviationDegrees);


            currentCooldown = fireRate;
            UseAmmo(ammoPerShot);

            float distanceMultiplier =
                CalculateDamageMultiplierCurve(Vector3.Distance(barrelTransform.position, targetPoint));
            // define weapon damage details



            if (!isRemotelyFiring)
            {
                //create beam
                localFirstFire = true;
                CreateBeam();
                SetIsRemotelyFiring(true);
            }
            else
            {
                localFirstFire = false;
            }

            if (!HasAmmoToShoot() && !isRecharging)
            {
                // StartCoroutine(DoBonusRecharge(extraRechargeTimeOnDepletion));

                // Debug.Log("cease fire");
                CeaseFire();
            }
            else
            {
                RaycastHitDetails raycastTracerDetails = Fire_HitscanWeaponTracer(targetPoint);



                // if the raycast tracer details health field is not null, then damage em
                if (raycastTracerDetails.hasHealth)
                {
                    WeaponDamageDetails weaponDamageDetails = new WeaponDamageDetails(myNickName, myPlayerId, myTeamId,
                                                                                      damageType, baseDamage * distanceMultiplier, raycastTracerDetails.localHitPoint);


                    raycastTracerDetails.hitTransform.gameObject.GetComponentInParent <VehicleHealthManager>()
                    .TakeDamage(weaponDamageDetails);
                }
                // do the fire effect on our end


                // ------------ local firing procedure:
                // if we hit, then fire a ray effect playing hitsound on hit
                if (raycastTracerDetails.hasHealth && raycastTracerDetails.validTarget)
                {
                    FireBeamRoundEffectHit(raycastTracerDetails.worldHitPoint);
                }
                // if we miss, then fire a ray effect playing missound on hit
                else if (!raycastTracerDetails.hasHealth && raycastTracerDetails.validTarget)
                {
                    FireBeamRoundEffectMiss(raycastTracerDetails.worldHitPoint);
                }



                // -----------  remote firing procedure:
                // if optimisations are not enabled, for all other players:

                // if corrections are applied and we hit a target with health then fire a ray using FireHitscanRoundEffectCorrected, playing the hitsound on hit
                if (raycastTracerDetails.hasHealth && raycastTracerDetails.validTarget)
                {
                    int hitTeamId = raycastTracerDetails.hitTransform.GetComponentInParent <NetworkPlayerVehicle>()
                                    .teamId;
                    weaponPhotonView.RPC(nameof(FireBeamRoundEffect_RPC), RpcTarget.Others, true,
                                         raycastTracerDetails.worldHitPoint, true, raycastTracerDetails.localHitPoint, hitTeamId);
                }
                else if (raycastTracerDetails.validTarget)
                {
                    weaponPhotonView.RPC(nameof(FireBeamRoundEffect_RPC), RpcTarget.Others, true,
                                         raycastTracerDetails.worldHitPoint, false, raycastTracerDetails.localHitPoint, 0);
                }
                else
                {
                    weaponPhotonView.RPC(nameof(FireBeamRoundEffect_RPC), RpcTarget.Others, false,
                                         raycastTracerDetails.worldHitPoint, false, raycastTracerDetails.localHitPoint, 0);
                }
            }
        }

        // now deal with beam effects


        if (beam != null && gunnerPhotonView.IsMine)
        {
            if (localFirstFire && !instantBeam)
            {
                // calculate target point interpolated along targetPoint - barrelEndMuzzleTransform.position direction vector by value timeSinceLastShot / maxLerpTime
                ShotBeamAtPointFirstFireLerp(targetPoint);
            }
            else
            {
                ShootBeamInDir(barrelEndMuzzleTransform.position, (targetPoint - barrelEndMuzzleTransform.position));
            }
        }
    }
Esempio n. 4
0
    // Start is called before the first frame update
    // ---------------------- COPY THESE FUNCTIONS FOR EACH CHILD CLASS --------------------------------//
    // ---------------------- RPCs DO NOT INHERIT FROM PARENT ------------------------------------------//

    // method called by weaponController
    // we need a new version of this for every child class, otherwise the top level RPC will be called
    public override void Fire(Vector3 targetPoint)
    {
        if (CanFire() && gunnerPhotonView.IsMine)
        {
            targetPoint =
                CalculateFireDeviation(targetPoint, projectileDeviationDegrees);

            currentCooldown = fireRate;
            UseAmmo(ammoPerShot);
            float distanceMultiplier = CalculateDamageMultiplierCurve(Vector3.Distance(barrelTransform.position, targetPoint));
            // define weapon damage details



            // now fire the original true round on the host's end
            // this is the shot that deals damage
            // this shot also returns the hit transform of what we hit
            // depending on if useTracerHitCorrection is enabled, call either of the FireHitscanRoundEffect rpcs
            RaycastHitDetails raycastTracerDetails = Fire_HitscanWeaponTracer(targetPoint);

            // if the raycast tracer details health field is not null, then damage em
            if (raycastTracerDetails.hasHealth)
            {
                WeaponDamageDetails weaponDamageDetails = new WeaponDamageDetails(myNickName, myPlayerId, myTeamId, damageType, baseDamage * distanceMultiplier, raycastTracerDetails.localHitPoint);
                raycastTracerDetails.hitTransform.gameObject.GetComponentInParent <VehicleHealthManager>().TakeDamage(weaponDamageDetails);
                // var bh = Instantiate(bulletHoles[0],raycastTracerDetails.worldHitPoint, Quaternion.Euler(raycastTracerDetails.normalAngle * 360));
                //bh.transform.rotation.SetEulerAngles(raycastTracerDetails.hitTransform.rotation.eulerAngles);
                //  bh.transform.SetParent(raycastTracerDetails.hitTransform.gameObject.transform, true);
                // Debug.Log("aaaaaaaaaaa" + raycastTracerDetails.normalAngle * 360) ;
                // Debug.Log(bh.transform.rotation.eulerAngles);
            }
            // do the fire effect on our end


            // ------------ local firing procedure:
            // if we hit, then fire a ray effect playing hitsound on hit
            if (raycastTracerDetails.hasHealth && raycastTracerDetails.validTarget)
            {
                FireHitscanRoundEffect(raycastTracerDetails.worldHitPoint);
            }
            // if we miss, then fire a ray effect playing missound on hit
            else if (!raycastTracerDetails.hasHealth && raycastTracerDetails.validTarget)
            {
                FireHitscanRoundEffectMiss(raycastTracerDetails.worldHitPoint);
            }
            // if valid target is null, then fire a ray effect with no impact
            else
            {
                FireHitscanRoundEffectNoValidTarget(raycastTracerDetails.worldHitPoint);
            }
            // do camera shake
            ShakeCameras(cameraShakeAmplitude, cameraShakeDuration);
            shakeTimerCur = 0;



            // -----------  remote firing procedure:
            // if optimisations are not enabled, for all other players:
            if (!useRapidFireOptimisation)
            {
                // if corrections are applied and we hit a target with health then fire a ray using FireHitscanRoundEffectCorrected, playing the hitsound on hit
                if (raycastTracerDetails.hasHealth && raycastTracerDetails.validTarget && useTracerHitCorrection)
                {
                    int hitTeamId = raycastTracerDetails.hitTransform.GetComponentInParent <NetworkPlayerVehicle>().teamId;
                    weaponPhotonView.RPC(nameof(FireHitscanRoundEffectCorrected), RpcTarget.Others, raycastTracerDetails.localHitPoint, hitTeamId);
                }
                // if corrections are not applied and we hit a target, then fire a ray usig FireHitscanRoundEffect, playing hitsound on hit
                else if (raycastTracerDetails.hasHealth && raycastTracerDetails.validTarget && !useTracerHitCorrection)
                {
                    weaponPhotonView.RPC(nameof(FireHitscanRoundEffect), RpcTarget.Others, raycastTracerDetails.worldHitPoint);
                }
                // if we hit something without health, then fire effect at hitpoint playing missound
                else if (!raycastTracerDetails.hasHealth && raycastTracerDetails.validTarget)
                {
                    weaponPhotonView.RPC(nameof(FireHitscanRoundEffectMiss), RpcTarget.Others, raycastTracerDetails.worldHitPoint);
                }
                // if we hit no valid target (ie we hit the air), then just fire effect at hitpoint and instantiate no impact
                else
                {
                    weaponPhotonView.RPC(nameof(FireHitscanRoundEffectNoValidTarget), RpcTarget.Others, targetPoint);
                }
            }


            // if optimisations are enabled, then get the other clients to start firing pretend shots
            // this is useful for very rapid fire weapons where we don't really care where the bullets graphically go
            if (useRapidFireOptimisation)
            {
                // instantiate relevant hit impact on vehicle
                if (raycastTracerDetails.hasHealth && raycastTracerDetails.validTarget && useTracerHitCorrection)
                {
                    int hitTeamId = raycastTracerDetails.hitTransform.GetComponentInParent <NetworkPlayerVehicle>().teamId;
                    weaponPhotonView.RPC(nameof(FireHitscanCorrectedImpact), RpcTarget.Others, raycastTracerDetails.localHitPoint, hitTeamId);
                }
                // this bool is synchronised via rpc on cooldown
                // the fire effects will fire the gun graphically in its current direction as long as it is active, causing no actual damage
                SetIsRemotelyFiring(true);

                // now that this is enabled, the guest will just fire and look after itself
            }
        }
    }
    protected virtual void FireRPC_ProjectileWeapon(Vector3 targetPoint, string serializedDamageDetails)
    {
        WeaponDamageDetails weaponDamageDetails = JsonUtility.FromJson <WeaponDamageDetails>(serializedDamageDetails);

        //   parentRigidbody = transform.root.GetComponent<Rigidbody>();
        // debug function to fire weapon
        weaponAnimator.SetTrigger(primaryFireAnimatorTriggerName);
        //  Debug.Log("ProjectileWeapon class object has fired");

        // if we are the owner of the photonview, then fire the real projectile


        /*
         * PooledObject pooledProjectile =
         *  Pool.Instance.Spawn(projectilePrefab, barrelTransform.position, barrelTransform.rotation);
         *
         *
         * GameObject projectile = pooledProjectile.gameObject;
         */

        GameObject obj = projectilePrefab;
        //  StopProjectileCollisionsWithSelf(obj);
        GameObject projectile = Instantiate(obj, barrelEndMuzzleTransform.position, barrelEndMuzzleTransform.rotation);
        // StopProjectileCollisionsWithSelf(projectile);

        ProjectileScript projScript = projectile.GetComponent <ProjectileScript>();

        // set projscript stuff
        projScript.SetWeaponDamageDetails(weaponDamageDetails);
        projScript.ActivateProjectile(imapactParticle, missImpactParticle, projectileParticleEffectPrefab, impactParticleSound, impactParticleSoundMiss, imapactParticleVolume, missImpactParticleVolume);


        DoMuzzleFlashEffect();
        projectile.transform.LookAt(targetPoint);

        PlayAudioClipOneShot(weaponFireSound);
        projectile.GetComponent <PhysXRigidBody>().mass = projectileMass;
        // FIRE REAL PROJECTILE
        if (gunnerPhotonView.IsMine)
        {
            projScript.SetTrueProjectile(true);
            projectile.GetComponent <PhysXRigidBody>().AddForce(projectileSpeed * (projectile.transform.forward), ForceMode.VelocityChange);
            if (inheritVelocityFromVehicle)
            {
                projectile.GetComponent <PhysXRigidBody>().AddForce(parentRigidbody.velocity, ForceMode.VelocityChange);
            }
        }
        // add projectile settings
        // otherwise fire a lag compensated dummy projectile with no damage scripts enabled
        else
        {
            projScript.SetTrueProjectile(false);
            float ping = (PhotonNetwork.GetPing() * 1.0f) / 2;
            // update position by ping
            Vector3 newPos = projectile.transform.position + (projectile.transform.forward * (ping * 0.001f) * projectileSpeed);
            if (inheritVelocityFromVehicle)
            {
                newPos += parentRigidbody.velocity * ping * 0.001f;
            }

            projectile.transform.position = newPos;
            projectile.GetComponent <PhysXRigidBody>().AddForce(projectileSpeed * (projectile.transform.forward), ForceMode.VelocityChange);
        }

        Destroy(projectile, 4f);
        //pooledProjectile.Finish(4f);
    }
    // Start is called before the first frame update
    // ---------------------- COPY THESE FUNCTIONS FOR EACH CHILD CLASS --------------------------------//
    // ---------------------- RPCs DO NOT INHERIT FROM PARENT ------------------------------------------//

    // method called by weaponController
    // we need a new version of this for every child class, otherwise the top level RPC will be called
    public override void Fire(Vector3 targetPoint)
    {
        if (CanFire() && gunnerPhotonView.IsMine)
        {
            ShakeCameras(cameraShakeAmplitude, cameraShakeDuration);
            targetPoint =
                CalculateFireDeviation(targetPoint, projectileDeviationDegrees);

            currentCooldown = fireRate;
            UseAmmo(ammoPerShot);
            float distanceMultiplier = CalculateDamageMultiplierCurve(Vector3.Distance(barrelTransform.position, targetPoint));
            // define weapon damage details



            // now fire the original true round on the host's end
            // this is the shot that deals damage
            // this shot also returns the hit transform of what we hit
            // depending on if useTracerHitCorrection is enabled, call either of the FireHitscanRoundEffect rpcs
            RaycastHitDetails raycastTracerDetails = Fire_HitscanWeaponTracer(targetPoint);

            // if the raycast tracer details health field is not null, then damage em
            if (raycastTracerDetails.hasHealth)
            {
                WeaponDamageDetails weaponDamageDetails = new WeaponDamageDetails(myNickName, myPlayerId, myTeamId, damageType, baseDamage * distanceMultiplier, raycastTracerDetails.localHitPoint);
                raycastTracerDetails.hitTransform.gameObject.GetComponentInParent <VehicleHealthManager>().TakeDamage(weaponDamageDetails);
            }
            // do the fire effect on our end


            // ------------ local firing procedure:
            // if we hit, then fire a ray effect playing hitsound on hit
            if (raycastTracerDetails.hasHealth && raycastTracerDetails.validTarget)
            {
                FireHitscanRoundEffect(raycastTracerDetails.worldHitPoint);
            }
            // if we miss, then fire a ray effect playing missound on hit
            else if (!raycastTracerDetails.hasHealth && raycastTracerDetails.validTarget)
            {
                FireHitscanRoundEffectMiss(raycastTracerDetails.worldHitPoint);
            }
            // if valid target is null, then fire a ray effect with no impact
            else
            {
                FireHitscanRoundEffectNoValidTarget(raycastTracerDetails.worldHitPoint);
            }
            // do camera shake
            ShakeCameras(cameraShakeAmplitude, cameraShakeDuration);
            shakeTimerCur = 0;



            // -----------  remote firing procedure:
            // if optimisations are not enabled, for all other players:

            // if corrections are applied and we hit a target with health then fire a ray using FireHitscanRoundEffectCorrected, playing the hitsound on hit
            if (raycastTracerDetails.hasHealth && raycastTracerDetails.validTarget && useTracerHitCorrection)
            {
                int hitTeamId = raycastTracerDetails.hitTransform.GetComponentInParent <NetworkPlayerVehicle>().teamId;
                weaponPhotonView.RPC(nameof(FireHitscanRoundEffectCorrected), RpcTarget.Others, raycastTracerDetails.localHitPoint, hitTeamId);
            }
            // if corrections are not applied and we hit a target, then fire a ray usig FireHitscanRoundEffect, playing hitsound on hit
            else if (raycastTracerDetails.hasHealth && raycastTracerDetails.validTarget && !useTracerHitCorrection)
            {
                weaponPhotonView.RPC(nameof(FireHitscanRoundEffect), RpcTarget.Others, raycastTracerDetails.worldHitPoint);
            }
            // if we hit something without health, then fire effect at hitpoint playing missound
            else if (!raycastTracerDetails.hasHealth && raycastTracerDetails.validTarget)
            {
                weaponPhotonView.RPC(nameof(FireHitscanRoundEffectMiss), RpcTarget.Others, raycastTracerDetails.worldHitPoint);
            }
            // if we hit no valid target (ie we hit the air), then just fire effect at hitpoint and instantiate no impact
            else
            {
                weaponPhotonView.RPC(nameof(FireHitscanRoundEffectNoValidTarget), RpcTarget.Others, targetPoint);
            }
        }
    }