Shooting like in demo project Angry Bots.
Hello all, I would like to understand or just sample code with explains "Projectile Shooting". There is a Demo asset calls Unity Angry Bots https://www.assetstore.unity3d.com/en/#!/content/1917
I like the shooting in this project. I can see how bullets throws and flies over network. I have similar project that players must see their bullets. I made projectile shooting Instantiting game object with rigidbody commponent attached. I have tried many ways PhotonNetwork.Instantiate, OnSerialize...., not fully RPC (because I didn't understand fully RPC method). tried to observe rigidbody, transform. But results are not makes me happy. I found Angry bots project which has already integrated photon networking. But all codes are in JavaScript. Even JS I have tried to understand but all codes have connected with each other. there are too many codes that maakes happen shooting.
There is a link for my first question: http://forum.photonengine.com/discussion/7608/sync-projectile-shooting-with-photon-networking#latest
I hope that someone can help me. Thanks in advance.
I like the shooting in this project. I can see how bullets throws and flies over network. I have similar project that players must see their bullets. I made projectile shooting Instantiting game object with rigidbody commponent attached. I have tried many ways PhotonNetwork.Instantiate, OnSerialize...., not fully RPC (because I didn't understand fully RPC method). tried to observe rigidbody, transform. But results are not makes me happy. I found Angry bots project which has already integrated photon networking. But all codes are in JavaScript. Even JS I have tried to understand but all codes have connected with each other. there are too many codes that maakes happen shooting.
There is a link for my first question: http://forum.photonengine.com/discussion/7608/sync-projectile-shooting-with-photon-networking#latest
I hope that someone can help me. Thanks in advance.
0
Answers
Basic need to know about RPCs
-
- RPC means Remote Procedure Call. A remote Procedure is just a normal Method of a Class, marked with Attribute [PunRPC].
- Every PunBehaviour can have an unlimited amount of these remotely callable methods.
- Every GameObject with a PhotonView (the 'heart' of PUN) can have PunBehaviours containing Remote Procedures.
- Remote Procedure Calls (RPC) are used to call a Method in an other Instance (on an other Computer) of the calling PunBehviour.
Example: You got an GameObject Soldier which got an PhotonView and a PunBehaviour called SoldierController. The PunBehaviour is linked to the PhotonView. The PhotonView's ID is unique in your game room and is same on each Client running your game.There is a [PunRPC]-marked method PlayJumpMotion in the PunBehaviour.
Imagine you got 2 of the Soldier GameObject (Soldier A and B ). Soldier A got PhotonView ID 2001 and SoldierB got PhotonView ID 2002.
If you call the RPC, something like this happens internally (very simplified):
1) Soldier A (ID 2001) sends Message cointaining "I want to raise RPC SoldierController.PlayJumpMotion on every Instance of SoldierController linked to a PhotonView."
2) Server send the Message "Call Remote Procedure SoldierController.PlayJumpMotion" to every PhotonView.
3) Every PhotonView gets this Message and checks if they got an script SoldierController with PlayJumpMotion attached.
-> If true, the PhotonView calls this Method.
You see, no Magic. Just an Message sent over Network which tells the Client to call an Method of a PunBehaviour observed by an PhotonView.
Second script calls "ThrowBullets". ThrowBullets is my Shooting, Firing script. I have problem with it. I can see very good shooting if i shoot. But if there is another player shoots I can see his shooting like bad, not smooth, not exact shooting. Because I still don't know what to write and what observe with my photonView which has attached to "Bullet Prefab".
So there are codes:
//First Code which is ok. Player mover, rotate, animater //Second Code which shoots by projectile method
Just call a RPC on other clients, which handles bullet spawning, moving, animation, effects (localy, GameObject.Instantiatie, not PhotonNetwork.Instantiate).
Second here are some pseudocode inspirations on how i handle shooting via network:
1) If (player pressed fire key)
2) Call local Fire(void) function
-> Spawn the Bullet locally on Barrel position and apply velocity to rigidbody
-> Handle animations
-> Handle muzzleflash, sounds, and other shooting related effects
-> call RPC RemoteFire(void) on every machine, but not on this one
3) RPC RemoteFire() does exactly the same, but without calling the RPC ( because even my instantiated network players got same animation, barrel position, and such ... full body awareness rules)
4) it's your choice if MasterClient or the shooting player checks who or what got hit by the bullets
5) If bullet hits something, the master calls RPCs for damage, bullethole, hit effect whatever.
Essence of this pseudocode:
-> Using RPCs instead of Instantiation is important, for bandwidth
-> Let the other clients handle only the visual stuff (bullet movement, bullet flying effect, trail...)
-> But only one client (masterclient or the shooting player's client) checks who got hit and applies damage and such
Improvements:
-> For guns with a high rate of fire, even this system consumes to much bandwidth. In this case you just send a RPC when player start firing and one when it ends and let the clients handle everything between this events themselves. , instead of calling a RPC for each bullet fired. But damage calculation and such stays on masterclient/shooting player's client
Addition according your ThrowBullet.cs :
-> You got a var allowShoot, if you want next to zero bandwidth consuming, just sync this in OnPhotonSerializeView and let the magic happen
Addion #2:
-> Take a look on Exitgame's Angry Bots Multiplayer sample from assetstore (free, up to date)
Additon #3:
-> (from german to english) German software developer saying: "You see further standing on giant's shoulders."
Don't try to do everything yourself. When you are new to programming and software development at all, this is a common mistake people use to do. Learn programming by actually developing your game, Use frameworks, understand how they use to do things and make them fit your needs. There are tons of good (and well priced, some even free) solutions for making FPS/TPS games in unity. A few support Photon natively. Dont waster your time in reinventing things, when not nessecary.
I have implemented simple PunRPC. Its shows Players HUD (UI) both your and enemies. Code looks like this: I also created Cannon shooting script but It's a little bit hard for me to call visible bullet shooting with PunRPC. Please could you help me with this?
https://dropbox.com/s/r7janqrxwb1poyd/%D0%A1%D0%BD%D0%B8%D0%BC%D0%BE%D0%BA%20%D1%8D%D0%BA%D1%80%D0%B0%D0%BD%D0%B0%202016-05-08%20%D0%B2%200.45.19.png?dl=0
PhotonView with ID 1001 has no method "FireRocket" marked with the [PunRPC](C#) or @PunRPC(JS) property! Args: Vector3, Quaternion, Int32
UnityEngine.Debug:LogError(Object)
CannonPlayer is my Player object so I use my players photonView component here, that instead of adding new photon view component to child object. Please any help will be appreciated. thanks