• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
Question by Derthmonuter · Jul 17, 2013 at 07:09 PM · networkingmultiplayerrpcshootnetworkview

What is the best way to properly implement shooting over a network?

I know that there most likely is no universal "best way" and that it all comes down to the comfort a programmer has working with his chosen method, but I'd like to ask whether or not what I am currently doing is valid.

I have the following code for a player to control a simple player object.

 using UnityEngine;
 using System.Collections;
 
 public class ShipController : MonoBehaviour
 {
     float speed = 0.8f;
     float turnSpeed = 0.4f;
     private VariableScript ptrScriptVariable;
     
     void Start ()
     {
         ptrScriptVariable = (VariableScript)GetComponent (typeof(VariableScript));
         rigidbody.useGravity = false;
     }
     
     [RPC]
     void FireRocket ()
     {
         if (ptrScriptVariable.objBullet != null) {
             GameObject bulletClone = ptrScriptVariable.objBullet;
             Debug.Log ("Firing a rocket: " + transform.position + " , " + transform.rotation);
             Instantiate (bulletClone, transform.position + new Vector3 (0, 3, 0), transform.rotation); 
         } else {
             Debug.Log ("The bullet object is null!");
         }
     }
     
     void FixedUpdate ()
     {
         if (networkView.isMine) {        
             //Spacebar by default will make it move forward
             if (Input.GetButton ("Jump")) { 
                 rigidbody.AddRelativeForce (Vector3.forward * speed);
             }
             
             // A or left arrow to turn left, D or right arrow to turn right.
             rigidbody.AddRelativeTorque (0, (Input.GetAxis ("Horizontal")) * turnSpeed, 0);
             
             //If the user clicks the left mouse button.
             if (Input.GetMouseButtonDown (0)) {
                 networkView.RPC ("FireRocket", RPCMode.AllBuffered);
             }
         } else {
             enabled = false;
         }
     }
 }

Each of these player objects includes a NetworkView set to RDC mode, and are both spawned in through another Network Manager script. They spawn and can move together and see one another just fine. I also want to allow these players to shoot at one another, so I have a bullet prefab which I use as a projectile. Now, this prefab contains no NetworkView. Instead, I use the [RPC] code above to spawn and fire it for each player. In essence, every player is firing everyone else's bullets too.

Is this the standard way of doing it? Is it efficient?

I'd really like to hear anyone else's input or suggestions on this topic, thanks.

Comment

People who like this

0 Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Benproductions1 · Jul 18, 2013 at 02:41 AM 0
Share

This is a way of doing it. there is no standard for this.

Your method however, is not authoritative. I could technically decompile your code and stop anyone from ever shooting at me, however I'm constantly shooting at everyone else.
It's "hackable".

avatar image Anotheryeti · Jul 18, 2013 at 02:51 AM 0
Share

Also, the timing is not guaranteed with this method, which could cause clients to easily become out of sync with each other.

avatar image Benproductions1 · Jul 18, 2013 at 03:06 AM 0
Share

Well, you can't ever guarantee timing (not completely). You can build a huge system that is able to do it approximtely

avatar image Anotheryeti · Jul 18, 2013 at 03:21 AM 0
Share

I mean, any game that has a deterministic game engine can keep clients synced with each other purely with correct timing and inputs, i.e. Starcraft. Although those are fairly discretized timesteps.

We may be talking about different kinds of timing.

avatar image Benproductions1 · Jul 18, 2013 at 04:10 AM 0
Share

I was just talking about inaccuracies caused by pinging, you therefore can never completely sync the time between two sides of a connection. (You come pretty close though).
In an unstable connection (highly fluctuating latancy), there can be quite a bad margin of error for syncs.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by xt-xylophone · Jul 22, 2013 at 03:33 AM

During my first foray into this problem I sync'd the positions of all bullets in my game. It was 'accurate' but turned horrible when there were 4+ people all shooting alot.

My next step was to do what you've done, RPC to send the fact that ive shot to my 'clones' whom im hoping are in a fairly similar position and orientation as I am. This ran much smoother but SOMETIMES there was the chance that I could shoot, hit another player and from their point of view I had missed slightly! For basics, this is pretty sufficient.

My current method is simply to send some extra information with the RPC such as my position or rotation. Depending on the circumstance, to get it pretty much spot on you wont need all XYZ rot and pos. So maybe only send XZ pos and XY rot to lighten the traffic a bit. My clone would then shoot using the extra information I sent rather than just where they happen to be at that time.

Like other people have said, yes that is hack-able since everything is client side. An authoritative server would make sure that that doesn't happen. I've never gone into anything where I expected cheating nor really bothered by it if someone tried so my 3rd iteration is whats been working well for me :)

Comment

People who like this

0 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Benproductions1 · Jul 22, 2013 at 04:36 AM 0
Share

This only works in combination with lag-compensation, unless you are playing on a LAN.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

networkView not called on Child objects? 0 Answers

How can I send a mouse click to a server using an RPC? 0 Answers

Is server the sender of RPC? 0 Answers

Unity networking tutorial? 6 Answers

Synchronize network view for transform children 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges