- Home /
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.
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".
Also, the ti$$anonymous$$g is not guaranteed with this method, which could cause clients to easily become out of sync with each other.
Well, you can't ever guarantee ti$$anonymous$$g (not completely). You can build a huge system that is able to do it approximtely
I mean, any game that has a deter$$anonymous$$istic game engine can keep clients synced with each other purely with correct ti$$anonymous$$g and inputs, i.e. Starcraft. Although those are fairly discretized timesteps.
We may be talking about different kinds of ti$$anonymous$$g.
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.
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 :)
This only works in combination with lag-compensation, unless you are playing on a LAN.
Your answer
Follow this Question
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