[Updated] 2D Online Shooter Tutorial

Using this asset you will learn how to create your own 2D online shooter!

link package: 2D Online Shooter Tutorial | Tutorials | Unity Asset Store

Screenshot of an example of simulation, network behavior of players:

An example of a traffic load using multiple player clones whose behaviors are transmitted over the network:

List of features:
All of these settings are changeable in a Play Mode (using only one file) :

  • mobile and PC controls;
  • player settings ;
  • other settings(sparks,bonuses,field size);
  • network simulation (clones,atacking,moving,rushing);

Documentation describes:

  • how to use Photon Cloud to transmit data over the network;
  • how to avoid stack overflow due to the usage of RPC with PhotonTargets.AllBuffered;

If you have any questions, please do not hesitate to contact me: aleksandronto@gmail.com


Update 1.1 :
– The recharge time (the Settings file - shot cooldown) is now more accurately calculated.
– Fix red bag - During immortality, if you switch control from the keyboard to mobile control - a link to the Null.
– The joystick did not move by clicking on the top of the screen (Overlapped another UI).
– When clicked on the edge of the screen, the joystick would go behind the screen.
– The touch area moved together with the joystick.
– Fixed saving the Settings file after exiting from the Unity. Now there is a way that always works correctly.

This video shows an example of how to observe the behaviors of players that are transmitted over the network.
Thanks to this simulation, you do not need to ask someone to download the assembly so that he came to your room and played with you.

I also highly recommend testing this simulation with different speeds, with different respawn rate of bullets and different number of players. Due to this, you will approximately understand when a queue of transmitted data on the network occurs and when a packet transmission delay occurs. This is important to understand because in PUN(PhotonCloud) the limits come very early.

Has altered the script which shows the data of the transmitted traffic in the room. PhotonStatsGuiPerSecond.cs .
I advise you to enable it to understand more accurately - how many it is necessary to transfer a byte of traffic, so that after that queues and delays in the transmission of packets arise.

Update 1.2

Update File Settings :
Add rush simulation
Add rush_all,atack_all,move_all

Fixes :

  • Fixed bug - While pressing Fire1 in FixedUpdate() sometimes returns a Input.GetButtonDown(“Fire1”) == false
  • Did not display the game icons in the installed android build on the mobile device
  • Removed function fix() from the script ConnectRoom
  • The variable bullet_speed did not work in the Settings file
  • Fix points spawn bonuses
  • Fixed red bug PUN - "Ev Destroy Failed. Could not find PhotonView with instantiationId "

For example, thanks to this simulation, we can consider the temporal paradoxes in the network

Update 1.3

Update File Settings :
Add amount clones
Add amount bonuses
Add field size

Fixes:

  • Now immortality can be changed in the play mode of the Unity through the file Settings.
  • Nickname of the player in the table will now be displayed correctly.
  • In the immortal, the simulation of the attack did not work
  • If you turn on rush simulation but turn off the simulation of the movement then the simulation of the rush did not work

By cloning players, we save CPU load. We open fewer ps builds, but we transfer more network traffic.
It’s also worth noting that opening four PC builds without clones and opening two PC builds with one clone is not the same amount of transmitted traffic. Although the number of objects transmitted over the network is the same(4==4). The fact is that updates(OnPhotonSerializeView) of photonoViews of one player are combined (all of its objects(PhotonNetwork.Instantiate)).

Hello, just purchased your asset and I’m very excited.
I’d like to load a new scene with scene manager when my player is destroyed.
In which script/position should i add this?

2DOnlineShooter - Scripts - Multiplayer - PlayerBehavior
This function(destroy_player) is called to all players.

3092118--233284--upload_2017-6-2_5-37-14.png

In the blue square, the one who dies. (The owner of the prefab player) In this condition, load the scene.
In the red square, the one who killed this player (the one who sent this RPC destroy_player to everyone)
As for the top lines(174-177), then all players must fulfill them because all players must see the transition of the player’s state to a state of death.

Hello and thanks for your reply:
I get this error: when player is killed:

Ev Destroy Failed. Could not find PhotonView with instantiationId 1011. Sent by actorNr: 1
UnityEngine.Debug:LogError(Object)
NetworkingPeer:OnEvent(EventData) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:2541)
ExitGames.Client.Photon.PeerBase:smile:eserializeMessageAndCallback(Byte[ ])
ExitGames.Client.Photon.EnetPeer:smile:ispatchIncomingCommands()
ExitGames.Client.Photon.PhotonPeer:smile:ispatchIncomingCommands()
PhotonHandler:Update() (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonHandler.cs:157)

i added this in player beahvior:
Invoke(“Destroy”, 1f);// allow time for playing the explosion animation
SceneManager.LoadScene(“start”); // ADDED THIS LINE

This red error tells you about the conflict in the line PhotonNetwork.Destroy. When you exit the scene, links to deleted objects are lost and PUN does not understand what to delete.

Yellow warnings of the incomplete sending of the entire stack of calls to the RPC functions can also occur.
(“Received RPC “name_function_rpc” for viewID but this PhotonView
does not exist! Was remote PV. Remote called. By: 1 Maybe GO was
destroyed but RPC not cleaned up.”)

To completely avoid these errors, you need to write the following -

3097121--233769--answer.png

Hello again,
ist works, when player is killed he is moved to my start screen.
From my start screen i’d like to run the scene “game” again, but the player does not join the room. It stops at “joinedLobby”:

Script “ConnectRoom” does not load the room.
To leave the room to load another scene and go back into the room, I prepared a code for you -


It is necessary to remove one conflict. Edit script “BoxSprite”

Thank you very much!

Thank you for buying my package =)

P.S : Who buys my packages do not hesitate to ask questions!
I will be happy to help you quickly navigate the content of the package.And for any questions on the topic of PUN, too, I answer =)

Is there any solution/tutorial to add an AI (bots) to the game for the case that not many player are online?

https://www.assetstore.unity3d.com/en/#!/content/86022
In the video package, you can see how I transfer much fewer bytes of traffic over the network using my methods. Thanks to the latest update(1.1), reduction dictionaries reduced the transfer of 200 bots to a transmission size that does not exceed MTU(1200 bytes) . ByteCountLastOperation ~ 1000 - 1100 bytes
p.s . : In the package documentation it is written that such MTU.

Hello, is your asset
2D Online Full
mobile ready?

In the first updates of this package, I will not add mobile management. I will expand the role-based part in it.
I’m currently working on another project (“Race Online Example”). I’m sorry.

You bought my 2d online shooter tutorial package, right?
Copy the control from there. What is the problem?
– script CrossPlatformInputManager Initializes management(pc or moblie) Lines 15-35
The response of pressing the buttons does not get out in the usual way(Input.GetAxis(“Horizontal”)), but through the class CrossPlatformInputManager.GetAxis(“Horizontal”)

hey i need help with a script of mine as when the player respawns multiple versions of player 1 then respawn XD here is the script:

void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.tag == "Player")
                other.gameObject.GetComponent<Animation> ().Play ("Death");

                Invoke ("Destroy", 3f);
    }

    void Destroy()
    {
        FindObjectOfType<ConnectRoom>().OnJoinedRoom();
        PhotonNetwork.Destroy(gameObject); 
    }

i put this on my death trap game object.

In the OnTriggerEnter2D function, you hit more than once.
You need to get into the OnTriggerEnter2D function once

    bool flag;
    void OnTriggerEnter2D(Collider2D other)
    {
        if (flag)return;
        flag = true;
        if (other.gameObject.tag == "Player")
                other.gameObject.GetComponent<Animation> ().Play ("Death");

                Invoke ("Destroy", 3f);
    }

    void Destroy()
    {
        FindObjectOfType<ConnectRoom>().OnJoinedRoom();
        PhotonNetwork.Destroy(gameObject);
    }

Once the trap has been activated, it is necessary again flag = false;
So that after the spawn you can again fall into the trap

only problem is now the player can’t rush and there is still a copy of player 1 (or any player for that matter) running around