How would I do this in PUN?

Options
I have a mobile game where the local player is always on the bottom of the screen next to the game controls, but I'm trying to figure out how to make them show up on one of three spawn points (left, top, or right) on everyone else's screen when they're not the local player, the gameobject they control is a turret and the only thing that needs to be sent is the rotation since their position will be different and will always be on the bottom on their own screens.

I've tried using PhotonNetwork.Instaniate and then setting up PhotonView on the gameobject but when I instantiate them on their main position (bottom), it instantiates all the players on the bottom.

Since that didn't work I tried instead to instaniate the gameobject locally on Unity and then for each player that's in the room it would instantiate another gameobject locally starting on the left spawnPoint and going clockwise, but the problem with doing this is that the photonview owner is set to "scene" and I can't get the cannon's rotation. Am I going about this the wrong way? I'm new to PUN and still trying to wrap my head around how everything fits together.

Comments

  • Hi @yaezah,

    I guess the easy way would be to let each client instantiate their object at their spawn point and then adjust the camera, so that each client's object is on the bottom position of their screen, even though it isn't the bottom position in world coordinates.

    [...] but the problem with doing this is that the photonview owner is set to "scene" and I can't get the cannon's rotation.


    If you are going this way, each client has to request Ownership of one of the objects. To see how this works, you can have a look at the Ownership Transfer documentation page as well as the related demo that is included in the PUN package.
  • yaezah
    yaezah
    edited April 2018
    Options
    @Christian_Simon

    Thanks for the advice, I have part of the code working to where if the photonview of the other object isn't mine it goes to a different spawn location. Here's what I have so far:

    [code]public class setPosition : Photon.MonoBehaviour {


    // Use this for initialization
    void Start () {

    Vector3 position = GameObject.Find("Network Manager").GetComponent ().spawnPoint[0].position;
    Vector3 position2 = GameObject.Find("Network Manager").GetComponent ().spawnPoint[1].position;
    if (photonView.isMine) {

    transform.position = new Vector3(position.x, position.y, position.z );
    GetComponent ().enabled = true;
    GetComponent ().enabled = true;
    GetComponent ().enabled = true;

    } else if (!photonView.isMine) {
    transform.position = new Vector3(position2.x, position2.y, position2.z);
    }
    }

    // Update is called once per frame
    void Update () {

    if (!photonView.isMine) {
    shootTowardsMouse ();
    }
    }

    void shootTowardsMouse(){
    //Somewhere here I need to find out how to set each cannon to it's respective player so that they're only getting coordinates from the player they belong to.
    Vector3 mousePoint = Camera.main.ScreenToWorldPoint (Input.mousePosition);
    transform.up = Vector3.Normalize (mousePoint + Vector3.forward * 10 - transform.position);

    }
    }[/code]

    The game is arcade type where the camera doesn't move at all and there's just 1-4 turrets at each side of the screen that shoot at randomly spawned enemies. I did end up using Photon.Instantiate to set the owner automatically and that seems to work. What I'm trying to do now is when I shoot with my local turret , it sends an RPC with coordinates to mouseposition where I shot and the relevant gameobject on the other player's screen rotates towards those coordinates and shoots. I'm trying to stay away from using photon tranform view to sync rotation because although the turret might be at the bottom of my screen it is probably on the top or left side of the other players screen and it ends up shooting the wrong direction. My question is how do I tell the other player that the cannon on the top side or other spawn positions belongs to me and not one of the other 2 players and to point it towards the mouseposition I shot at ?
  • My question is how do I tell the other player that the cannon on the top side or other spawn positions belongs to me and not one of the other 2 players and to point it towards the mouseposition I shot at ?


    If you are using PhotonNetwork.Instantiate all clients know who the owner of the object is. In code you can get a reference to the PhotonView component of the object and access the 'owner' property of it. This returns a PhotonPlayer.

    If you now want to shoot, you have to transform the mouse position into a world position and share this information when using the RPC. This step is necessary if you have rotated the camera. Whenever a client now clicks a mouse button, you have to do a Raycast that have to hit something on the 'ground'. This can for example be an invisible Trigger Box. Here you can find some code snippets of such an implementation. When you have this world position, you can send it with the RPC. Instead of the mouse position, the world position is the same on all clients,