Learning resource for networking?
Hey,
i did the tutorial on networking. Im now looking for more as the one tutorial is insufficient. it says we'll learn about interpolation and advanced techniques in an other topic, but i dont think it exists.
when googling i could only find others having the same trouble, most questions without an accepted answer.
where should i look?
in detail, i am trying to make a simple coop 2d shooter, just to learn networking. one client is acting as server. when shooting on the non-server-client, the bullet spawns with a noticable delay (even when executing on the same machine). i want the client to just spawn its own bullet localy, then tell the server about it. i dont care about cheating.
guess thats kind of a common objective, i dont get why there seems to be no resource about this.
help :(
edit: i dont need a specific answer here; i am mostly looking for links to advanced networking tutorials.
edit2: i asked about this, phrased differently, some time ago. got no answer. cant find resource. i love unity, but networking is realy hard and i need additional advice!
Answer by robbel · Oct 15, 2017 at 01:43 PM
Okay,
so i couldnt find any resource on this, but i managed to get the visual part "right".
Spoiler: solution is dirty (the bad kind).
I support 2 players only. One player is acting as host, so ill call him just that. The other player i will call client (tho technically both are clients ait?).
When the host shoots, everythings fine. Just spawn the bullet acording to the networking tutorial. However, when the client fires, i localy spawn a bullet, and then tell the server to spawn an equal bullet (wich i call clone henceforth). I attached a script to the bullet that disables the renderer, network transform and circular collider of the bullet when spawned on the client and is a clone.
just look at this mess:
/* player controller */
void Update()
{
if (!isLocalPlayer)
{
return;
}
//(some code to move the player and set direction)
if (Input.GetKeyDown(KeyCode.Space))
{
Fire(direction);
}
}
void Fire (Vector2 dir)
{
// spawn bullet weather client or host
var bullet = SpawnBullet(dir);
if (isServer)
{
// case 1: Host firing
// just spawn the bullet
NetworkServer.Spawn(bullet);
}
else
{
// case 2: Client firing
// command server to spawn a copy of the bullet, but disable most of it when its replicated on the client that shot.
CmdClone(dir);
}
}
[Command]
void CmdClone(Vector2 dir)
{
var clone = SpawnBullet(dir);
clone.GetComponent<ClientSpawn>().isClone = true;
NetworkServer.Spawn(clone);
}
GameObject SpawnBullet(Vector2 dir)
{
var bullet = (GameObject)Instantiate(bulletPrefab, bulletSpawn.position, bulletSpawn.rotation);
bullet.GetComponent<Bullet>().ownerId = id.get();
bullet.GetComponent<Rigidbody2D>().velocity = dir * 15;
Destroy(bullet, 2.0f);
return bullet;
}
/* script attached to bullet */
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class ClientSpawn : NetworkBehaviour
{
[SyncVar]
public bool isClone = false;
void Start()
{
if (isClone && !isServer)
{
GetComponent<SpriteRenderer>().enabled = false;
GetComponent<NetworkTransform>().enabled = false;
GetComponent<CircleCollider2D>().enabled = false;
}
}
}
But will it blend?
Im curious if i am able to get the destruction of the bullet and its clone synced properly, and so on.
I Obviously still need help, and if this continues, i will develope some mental disorder, maybe multiple.
Your answer
Follow this Question
Related Questions
Detect specific game object with raycast and show UI when hit 2 Answers
Is the customizable character tutorial still out there somewhere? 2 Answers
Unity 3D textbook / ebook 0 Answers
Multiplayer FPS version of unity networking tutorial? Using rigidbodies- see project link 0 Answers
Roll a ball Movement. 0 Answers