Help to send a bullet in a direction that a client is facing

Hey Unity community

I’m Morten, a rookie at developing Multiplayer games so I thought I should come and ask you guys for help. First of I would like to apologize if this is the incorrect subforum. If that’s the case, please point me in the direction of the right one to use :slight_smile:

So, I wanted to learn how to make Multiplayer games in the UnityEngine, so I found an “easy” game idea that I could practise networking on. Some of you might know the well-known game diep.io? I thought I would recreate something like that just in a style of capturing the flag.
Anyways enough background story.

I’m struggling at bit with all the Attributes that comes from NetworkBehavior.
I want to sync the position of the bullet and the direction they are being fired. After a lot of debuggin I came to a conclusion that there is a flaw in the way I handle the bullet spawning

using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
using UnityEngine.Networking;
using System.Collections;

public class PlayerShoot : NetworkBehaviour
{

    public float damage = 20f;
    public float speed = 300f;
    [Tooltip("This is how long the bullets should live in seconds. When Time.time has exceeded that, they will be destroyed")]
    public float bulletLifeInSeconds = 20f;
    public GameObject bullet;

    private Transform mainTransform;
    private Camera playerCamera;

    void Start()
    {
        playerCamera = GetComponentInChildren<Camera>();
        mainTransform = GetComponent<Transform>();
    }

    // Update is called once per frame
    void Update()
    {
        checkIfShooting();
    }

    void checkIfShooting()
    {
        if (!isLocalPlayer)
        {
            return;
        }

        if (CrossPlatformInputManager.GetButtonDown("Fire1"))
        {
            Cmdfire();
        }
    }

    [Command] // Invoked on the server by a client sending a command
    void Cmdfire()
    {
        RpcFireBall();
    }

    [ClientRpc] // Invoked on clients from Server
    void RpcFireBall()
    {
        Vector2 direction = playerCamera.ScreenToWorldPoint(Input.mousePosition) - mainTransform.position;
        direction.Normalize();
        GameObject theBullet = Instantiate(bullet, mainTransform.position + (new Vector3(direction.x, direction.y, 0) * 5), Quaternion.identity) as GameObject;
        theBullet.GetComponent<Rigidbody2D>().velocity = direction * speed;
        theBullet.GetComponent<Bullet>().owner = gameObject;

        theBullet.transform.parent = GameObject.Find("Bullets").transform;
    }
}

If you look at the two bottom methods, it makes sense that the result of that would look like:

As you can see, the positions and rotations doesn’t sync when a bullet is shot… Because that’s what I’m telling my code to do. I’m telling the code to do the “RpcFireBall()” method in the client, so of course the bullet is send the way the client is currently facing since it is the client’s instance of the code I’m telling the server to call (if I’m not mistaking)

So the question is, how would I go about sending the bullets in the correct direction so it would be the same for all clients?

Thanks for the help in advance :slight_smile:

  • Morten

Due to the Unity forum moderators taking so long to publicly expose my Thread, I had to do the googling myself.
Solved it by calculating the directing on the client [ClientCallBack] that then calls a command which updates a Syncvar to expose the direction for the server