0
\$\begingroup\$

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);

    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.