Beispiel #1
0
        //check what was hit on collisions
        void OnTriggerEnter(Collider col)
        {
            //cache corresponding gameobject that was hit
            GameObject obj = col.gameObject;
            //try to get a player component out of the collided gameobject
            Player player = obj.GetComponent <Player> ();

            //we actually hit a player
            //do further checks
            if (player != null)
            {
                //ignore ourselves & disable friendly fire (same team index)
                if (player.gameObject == owner || player.gameObject == null)
                {
                    return;
                }
                //else if (player.teamIndex == owner.GetComponent<Player>().teamIndex)
                //    return;

                //create clips and particles on hit
                if (hitFX)
                {
                    PoolManager.Spawn(hitFX, transform.position, Quaternion.identity);
                }
                if (hitClip)
                {
                    AudioManager.Play3D(hitClip, transform.position);
                }

                //on the player that was hit, set the killing player to the owner of this bullet
                //maybe this owner really killed the player, but that check is done in the Player script
                player.killedBy = owner;
            }

            if (isServer)
            {
                PoolManager.Despawn(gameObject, 0f);
            }


            //apply bullet damage to the collided player
            if (player)
            {
                player.TakeDamage(this);
            }


            //var hit = col.gameObject;
            //var health = hit.GetComponent<Player>();
            //if (health != null)
            //{
            //    health.TakeDamage(this);
            //}
            //despawn gameobject

            //Destroy(gameObject);

            //PoolManager.Despawn (gameObject);
        }
Beispiel #2
0
        //set initial travelling velocity
        //On Host, add automatic despawn coroutine
        void OnSpawn()
        {
            //for bouncing bullets, save current position only on first spawn (turret position)
            if (bounce == maxBounce)
            {
                lastBouncePos = transform.position;
            }

            myRigidbody.velocity = speed * transform.forward;
            PoolManager.Despawn(gameObject, despawnDelay);
        }
Beispiel #3
0
        public void Destroy()
        {
            //despawn object and clear references
            PoolManager.Despawn(obj);
            obj = null;

            //if it should respawn again, trigger a new coroutine
//			if(PhotonNetwork.isMasterClient && respawn)******
//            {
//                StartCoroutine(SpawnRoutine());
//            }
        }
Beispiel #4
0
        public void Destroy()
        {
            //despawn object and clear references
            PoolManager.Despawn(obj);
            NetworkServer.UnSpawn(obj);
            obj = null;

            //if it should respawn again, trigger a new coroutine
            if (respawn)
            {
                StartCoroutine(SpawnRoutine());
            }
        }
Beispiel #5
0
        //check what was hit on collisions
        void OnTriggerEnter(Collider col)
        {
            //cache corresponding gameobject that was hit
            GameObject obj = col.gameObject;
            //try to get a player component out of the collided gameobject
            Player player = obj.GetComponent <Player>();

            //we actually hit a player
            //do further checks
            if (player != null)
            {
                //ignore ourselves & disable friendly fire (same team index)
                if (player.gameObject == owner || player.gameObject == null)
                {
                    return;
                }
                else if (player.teamIndex == owner.GetComponent <Player>().teamIndex)
                {
                    return;
                }

                //create clips and particles on hit
                if (hitFX)
                {
                    PoolManager.Spawn(hitFX, transform.position, Quaternion.identity);
                }
                if (hitClip)
                {
                    AudioManager.Play3D(hitClip, transform.position);
                }

                //on the player that was hit, set the killing player to the owner of this bullet
                //maybe this owner really killed the player, but that check is done in the Player script
                //if (NetworkServer.active == true)
                //    player.killedBy = owner;
            }


            if (NetworkServer.active == true)
            {
                //apply bullet damage to the collided player
                //no need to check if the player is team mate since it will return above if it is a team mate.
                if (player)
                {
                    player.TakeDamage(this);
                }
            }

            PoolManager.Despawn(gameObject);
        }
Beispiel #6
0
        //play particles
        void OnSpawn()
        {
            //loop over ParticleSystem references and play them
            //Unity does not seem to calculate a new iteration of particles when
            //particles get activated, so here we add a randomized seed to it too
            for (int i = 0; i < pSystems.Length; i++)
            {
                pSystems[i].randomSeed = (uint)Random.Range(0f, uint.MaxValue);
                pSystems[i].Play();
            }

            //set automatic despawn after play duration
            PoolManager.Despawn(gameObject, delay);
        }
        /// <summary>
        /// Play sound clip passed in in 3D space, with optional random pitch (0-1 range).
        /// Automatically creates an audio source for playback using our PoolManager.
        /// </summary>
        public static void Play3D(AudioClip clip, Vector3 position, float pitch = 0f)
        {
            //cancel execution if clip wasn't set
            if (clip == null)
            {
                return;
            }
            //calculate random pitch in the range around 1, up or down
            pitch = UnityEngine.Random.Range(1 - pitch, 1 + pitch);

            //activate new audio gameobject from pool
            GameObject audioObj = PoolManager.Spawn(instance.oneShotPrefab, position, Quaternion.identity);
            //get audio source for later use
            AudioSource source = audioObj.GetComponent <AudioSource>();

            //assign properties, play clip
            source.clip  = clip;
            source.pitch = pitch;
            source.Play();

            //deactivate audio gameobject when the clip stops playing
            PoolManager.Despawn(audioObj, clip.length);
        }
Beispiel #8
0
        //check what was hit on collisions
        void OnTriggerEnter(Collider col)
        {
            //cache corresponding gameobject that was hit
            GameObject obj = col.gameObject;

            //try to get a player component out of the collided gameobject
            Player player = obj.GetComponent <Player>();

            ZoneTakedamage Zonedamage = obj.GetComponent <ZoneTakedamage>();

//            PlayerBot bot = obj.GetComponent<PlayerBot>();
            //we actually hit a player
            //do further checks
            if (player != null)
            {
                //ignore ourselves & disable friendly fire (same team index)
                if (player.gameObject == owner)
                {
                    return;
                }
                else if (obj.tag == owner.tag)
                {
                    return;
                }

//                else if (player.GetView().GetTeam() == owner.GetComponent<Player>().GetView().GetTeam()) return;
//                else if (null != bot && bot == owner.GetComponent<PlayerBot>()) return;

                //create clips and particles on hit
                if (hitFX)
                {
                    PoolManager.Spawn(hitFX, transform.position, Quaternion.identity);
                }
                if (hitClip)
                {
                    AudioManager.Play3D(hitClip, transform.position);
                }

                //on the player that was hit, set the killing player to the owner of this bullet
                //maybe this owner really killed the player, but that check is done in the Player script
                player.killedBy = owner;
            }
            else if (bounce > 0)
            {
                //a player was not hit but something else, and we still have some bounces left
                //create a ray that points in the direction this bullet is currently flying to
                Ray        ray = new Ray(transform.position - transform.forward, transform.forward);
                RaycastHit hit;

                //perform spherecast in the flying direction, on the default layer
                if (Physics.SphereCast(ray, sphereCol.radius, out hit, speed, 1 << 0))
                {
                    //something was hit in the direction this projectile is flying to
                    //get new reflected (bounced off) direction of the colliding object
                    Vector3 dir = Vector3.Reflect(ray.direction, hit.normal);
                    //rotate bullet to face the new direction
                    transform.rotation = Quaternion.LookRotation(dir);
                    //reassign velocity with the new direction in mind
                    OnSpawn();

                    //play clip at the collided position
                    if (hitClip)
                    {
                        AudioManager.Play3D(hitClip, transform.position);
                    }
                    //substract bouncing count by one
                    bounce--;
                    //exit execution until next collision
                    return;
                }
            }
            else if (Zonedamage)
            {
                return;
            }
            else if (col.tag == "Door")
            {
                return;
            }

            //despawn gameobject
            PoolManager.Despawn(gameObject);

            //the previous code is not synced to clients at all, because all that clients need is the
            //initial position and direction of the bullet to calculate the exact same behavior on their end.
            //at this point, continue with the critical game aspects only on the server
            if (!PhotonNetwork.isMasterClient)
            {
                return;
            }
            //apply bullet damage to the collided player
            if (player)
            {
                player.TakeDamage(this);
            }
        }
Beispiel #9
0
 //set initial travelling velocity
 //On Host, add automatic despawn coroutine
 void OnSpawn()
 {
     myRigidbody.velocity = speed * transform.forward;
     PoolManager.Despawn(gameObject, despawnDelay);
 }
        //set initial travelling velocity
        //On Host, add automatic despawn coroutine
        void OnSpawn()
        {
//            Vector3 direction = Player.shotPos2.transform.forward;
//            Vector3 u = v * direction;
            PoolManager.Despawn(gameObject, despawnDelay);
        }
Beispiel #11
0
 //check what was hit on collisions
 void OnTriggerEnter(Collider col)
 {
     //despawn gameobject
     PoolManager.Despawn(gameObject);
 }
Beispiel #12
0
        ///check what was hit on collisions. Only do non-critical client work here,
        //not even accessing player variables or anything like that. The server side is separate below
        void OnTriggerEnter(Collider col)
        {
            //cache corresponding gameobject that was hit
            GameObject obj = col.gameObject;
            //try to get a player component out of the collided gameobject
            Player player = obj.GetComponent <Player>();

            //we actually hit a player
            //do further checks
            if (player != null)
            {
                //ignore ourselves & disable friendly fire (same team index)
                if (IsFriendlyFire(owner.GetComponent <Player>(), player))
                {
                    return;
                }

                //create clips and particles on hit
                if (hitFX)
                {
                    PoolManager.Spawn(hitFX, transform.position, Quaternion.identity);
                }
                if (hitClip)
                {
                    AudioManager.Play3D(hitClip, transform.position);
                }
            }
            else if (bounce > 0)
            {
                //a player was not hit but something else, and we still have some bounces left
                //create a ray that points in the direction this bullet is currently flying to
                Ray        ray = new Ray(lastBouncePos - transform.forward * 0.5f, transform.forward);
                RaycastHit hit;

                //perform spherecast in the flying direction, on the default layer
                if (Physics.SphereCast(ray, sphereCol.radius, out hit, Mathf.Infinity, 1 << 0))
                {
                    //ignore multiple collisions i.e. inside colliders
                    if (Vector3.Distance(transform.position, lastBouncePos) < 0.05f)
                    {
                        return;
                    }

                    //cache latest collision point
                    lastBouncePos = hit.point;
                    //substract bouncing count by one
                    bounce--;

                    //something was hit in the direction this projectile is flying to
                    //get new reflected (bounced off) direction of the colliding object
                    Vector3 dir = Vector3.Reflect(ray.direction, hit.normal);
                    //rotate bullet to face the new direction
                    transform.rotation = Quaternion.LookRotation(dir);
                    //reassign velocity with the new direction in mind
                    OnSpawn();

                    //play clip at the collided position
                    if (hitClip)
                    {
                        AudioManager.Play3D(hitClip, transform.position);
                    }
                    //exit execution until next collision
                    return;
                }
            }

            //despawn gameobject
            PoolManager.Despawn(gameObject);

            //the previous code is not synced to clients at all, because all that clients need is the
            //initial position and direction of the bullet to calculate the exact same behavior on their end.
            //at this point, continue with the critical game aspects only on the server
            if (!PhotonNetwork.IsMasterClient)
            {
                return;
            }

            //create list for affected players by this bullet and add the collided player immediately,
            //we have done validation & friendly fire checks above already
            List <Player> targets = new List <Player>();

            if (player != null)
            {
                targets.Add(player);
            }

            //in case this bullet can hit more than 1 target, perform the additional physics area check
            if (maxTargets > 1)
            {
                //find all colliders in the specified range around this bullet, on the Player layer
                Collider[] others      = Physics.OverlapSphere(transform.position, explosionRange, 1 << 8);
                Player     ownerPlayer = owner.GetComponent <Player>();

                //loop over all player collisions found
                for (int i = 0; i < others.Length; i++)
                {
                    //get Player component from that collision
                    Player other = others[i].GetComponent <Player>();
                    if (other == null || targets.Contains(other))
                    {
                        continue;
                    }

                    //again, ignore own bullets and also friendly fire, now done exclusively on server side
                    if (IsFriendlyFire(ownerPlayer, other))
                    {
                        continue;
                    }

                    //add this Player component to the list
                    //cancel in case we do reach the maximum count now
                    targets.Add(other);
                    if (targets.Count == maxTargets)
                    {
                        break;
                    }
                }
            }

            //apply bullet damage to the collided players
            for (int i = 0; i < targets.Count; i++)
            {
                targets[i].TakeDamage(this);
            }
        }
Beispiel #13
0
 /// <summary>
 /// On Host, add automatic despawn coroutine
 /// </summary>
 public override void OnStartServer()
 {
     PoolManager.Despawn(gameObject, despawnDelay);
 }