Multiplayer game- Client Shooting backwards

by Omari Lawrence   Last Updated March 14, 2019 19:13 PM - source

Making a multiplayer game. The Host shoots properly but the other players would only shoot backwards.

Shooting script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class spawner_net : NetworkBehaviour
{
    public GameObject eggPrefab;
    public Transform cam;
    public float offset;
    GameObject eggInstance;
    public Animator anim;
    public float power = 300;
    private Vector3 front;
    private Transform trans;

    // Start is called before the first frame update
    void Start()
    {
        anim = GetComponent<Animator>();
        trans = transform;
    }

    // Update is called once per frame
    void Update()
    {
        front = cam.forward;

        if (isLocalPlayer)
        {
            if (cam == null)
            {
                Debug.Log("how");
            }
            if (Input.GetMouseButtonDown(0))
            {
                if (isServer)
                {
                    Debug.Log("Shooting from server");
                }else if (isClient)
                {
                    Debug.Log("Shooting from client");
                }
                else
                {
                    Debug.Log("Shooting from the void");
                }
                anim.SetBool("throwing", true);
                CmdSpawnEgg();

            }
            else
            {
                anim.SetBool("throwing", false);
            }
        }
    }

    [Command]
    void CmdSpawnEgg()
    {

        GameObject instance = Instantiate(eggPrefab, trans.position + (trans.forward * offset), cam.rotation);
        instance.GetComponent<Rigidbody>().AddForce(front * power);
        NetworkServer.Spawn(instance);

    }
}


Related Questions


Only host can spawn object

Updated March 13, 2019 15:13 PM

Networked projectiles in an authoritative server

Updated June 23, 2016 08:05 AM



Testing a Client's Camera in a Multiplayer FPS

Updated May 14, 2019 22:13 PM