Abhishek Joshi

Game Programmer


Resume Github Link

2D Multiplayer

About

This is a 2d multiplayer game where you and your teammate have to survive from enemies.

Project Info

My Role

This was my first multiplayer game. For multiplayer Networking, I used Photon PUN with Unity.

In this game both players are using Photon View and Photon transform View components that are responsible for Syncing the player movement and shooting through the network. For player shooting it is using a RPC function called RPC_Shoot that sending bulletAngle and _desiredBulletSpread as a parameter.

And FollowEnemy, ShootEnemy and LaserEnemy are inherited from EnemyController class in order to work. All the enemies are using Photon View.


Code snippets

                    

                        
                        private void Move()
                        {
                            transform.position += (Vector3)moveDirection * speed * Time.deltaTime;

                            transform.rotation = Quaternion.Euler(0, 0, -moveDirection.x * rotateAngle);
                        }

                        private void Shoot()
                        {
                            float desiredBulletSpread = UnityEngine.Random.Range(-bulletSpread, bulletSpread);

                            cameraShake.TriggerShake(0.15f, 0.15f);

                            view.RPC("RPC_Shoot", RpcTarget.All, GetBulletAngle(), desiredBulletSpread);
                        }

                        [PunRPC]
                        private void RPC_Shoot(float bulletAngle, float _desiredBulletSpread)
                        {
                            GameObject tempBullet = ObjectPool.instance.GetBullets();

                            if (!tempBullet) return;

                            tempBullet.GetComponent().bulletParent = BulletParent.Player;

                            popUp.Popup();

                            AudioManager.instance.PlaySoundEffect("Shooting");

                            tempBullet.transform.position = transform.position;

                            tempBullet.SetActive(true);

                            tempBullet.GetComponent().Init(bulletSpeed, _desiredBulletSpread, bulletAngle);
                            
                        }

                        public void TakeDamage(float value)
                        {
                            Health -= value;

                            if(view.IsMine)
                                UIManager.instance.UpdateHealthBar(Health);

                            popUp.Popup();

                            if (Health <= 0) Die();
                            else AudioManager.instance.PlaySoundEffect("Hit");
                        }

                        private void Die()
                        {
                            view.RPC("RPC_Die", RpcTarget.All);
                        }

                        [PunRPC]
                        public void RPC_Die()
                        {
                            AudioManager.instance.PlaySoundEffect("Die");

                            GameManager.instance.GameOver();
                            UIManager.instance.GameOver();
                        }

                    
                
                    

                        
                    public void Die()
                    {
                        view.RPC("RPC_Die", RpcTarget.All);
                    }

                    [PunRPC]
                    public void RPC_Die()
                    {
                        if (once >= 1)
                            return;

                        once++;

                        UIManager.instance.AddScore(50);

                        cameraShake.TriggerShake(0.2f, 0.35f);

                        AudioManager.instance.PlaySoundEffect("Die");

                        gameObject.SetActive(false);

                        GameObject deathParticle = ObjectPool.instance.GetDeathParticle();

                        if (deathParticle)
                        {
                            deathParticle.transform.position = transform.position;
                            deathParticle.GetComponent().Play();
                        }
                    }

                    public  void TakeDamage(float value)
                    {
                        Health -= value;

                        spriteRenderer.color = Color.white;

                        popUp.Popup();

                        Invoke("ResetColor", 0.075f);

                        if (Health <= 0) Die();
                        else AudioManager.instance.PlaySoundEffect("Hit");

                    }

                    public void SendInfo(bool active, Vector3 pos)
                    {
                        view.RPC("RPC_SendInfo", RpcTarget.AllBuffered, active, pos);
                    }

                    [PunRPC]
                    public void RPC_SendInfo(bool active, Vector3 pos)
                    {
                        gameObject.SetActive(active);
                        transform.position = pos;
                    }

                    public void ResetInfo()
                    {
                        view.RPC("RPC_ResetInfo", RpcTarget.AllBuffered);
                    }

                    [PunRPC]
                    public void RPC_ResetInfo()
                    {
                        Health = maxHealth;
                        once = 0;
                    }

                    
                

What I Learned

This was my first multiplayer game and I learned a lot about Photon and multiplayer Networking.

Talking about photon I learned about Photon View and Photon transform View components and how they works with gameObjects. Learned about RPC (Remote Procedure Calls) functions, like to send any information to all clients or masterclient.


Github Link