So I’m making a simple 2 player game following this guide: https://docs.unity3d.com/Manual/UNetSetup.html but instead of 3d shooting game I’m making 2d top-down game.
One of players is on the bottom of the screen and shoots up and the second player is on top of the screen and should shoot down. However I can’t figure out how do I make the second player shoot down. I tried to check if player is not a server and apply negative speed to its bullets but it doesn’t seem to work.
There’s a picture of what I’m working on for better understanding:

Here is my code:
Script attached to player - PlayerScript.cs:
using UnityEngine;
using UnityEngine.Networking;
public class PlayerScript : NetworkBehaviour {
public float speed = 5.0f;
public GameObject projectilePrefab;
public int rpm = 120;
private float lastTimeFired = 0;
private float timeBtwnShots;
void Start () {
timeBtwnShots = 60.0f / rpm;
}
public override void OnStartLocalPlayer() {
GetComponent<SpriteRenderer>().color = Color.black;
if (isServer) transform.Translate(new Vector2(0, -3));
else {
transform.Translate(new Vector2(0, 3));
transform.RotateAround(transform.position, Vector3.back, 180);
}
}
void Update () {
if (!isLocalPlayer) return;
if(Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
gameObject.transform.Translate(new Vector2(-speed * Time.deltaTime, 0));
else if(Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
gameObject.transform.Translate(new Vector2(speed * Time.deltaTime, 0));
if(Input.GetKey(KeyCode.Space)) {
CmdFire();
}
}
[Command]
void CmdFire() {
if (Time.time - lastTimeFired > timeBtwnShots) {
GameObject g = Instantiate(projectilePrefab, transform.position + new Vector3(0, 0.5f, 0), Quaternion.identity) as GameObject;
g.transform.parent = gameObject.transform;
lastTimeFired = Time.time;
NetworkServer.Spawn(g);
Destroy(g, 3.0f);
}
}
}
Script attached to fired projectiles - ProjectileScript.cs:
using UnityEngine;
public class ProjectileScript : MonoBehaviour {
public float speed = 5.0f;
void Start() {
if (transform.parent.gameObject.GetComponent<PlayerScript>().isServer == false)
speed *= -1;
}
void Update () {
transform.Translate(new Vector2(0, 1) * speed * Time.deltaTime);
}
}
I want client to shoot down. I thought this line in ProjectileScript.cs would make it:
if (transform.parent.gameObject.GetComponent<PlayerScript>().isServer == false) speed *= -1;, but it doesn’t work.
BTW I’ve noticed a strange thing with g.transform.parent = gameObject.transform; line: sometimes it just doesn’t work… Projectiles spawn without parent in objects hierarchy.