Writing to the stream for Non-Host

Options
Basically I have a vehicle that is controlled by the person who well, gets in the driver seat. The host will get into the driver seat and its that local clients job to track the physics (Rigidbody is only enabled for that client) and so it is their responsibility to properly update the vehicle along all screens. The problem of course now being that when the non-host gets in the seat, they can't send the vehicles info to the stream, they can only receive it. (This I BELIEVE (not sure) is due to the fact that the vehicle starts in the scene). So I am wondering if there is any way to make so the non-host player can write to the stream rather than just the host.

This is the script I am currently using to track the vehicle:

using System.Collections;

using UnityEngine;
using System.Collections;

public class TrackMyObject : Photon.MonoBehaviour {

Vector3 realPosition = Vector3.zero;
Quaternion realRotation = Quaternion.identity;

public float updateRatePos = 0.1f;
public float updateRateRot = 0.1f;

public bool syncPos;
public bool syncRot;

// Use this for initialization
void Start () {
realPosition = gameObject.transform.position;
}

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

if (photonView.isMine) {
// If this is my player - im sending info no need to receive
} else {
// If its not mine and im receiving the info
if (syncPos == true) {
transform.position = Vector3.Lerp (transform.position, realPosition, updateRatePos);
}
if (syncRot == true) {
transform.rotation = Quaternion.Lerp (transform.rotation, realRotation, updateRateRot);
}
}
}

public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info) {

// If we are the one sending the data
// In the case send the other players our info / position
if (stream.isWriting) {
if (syncPos == true) {
stream.SendNext (transform.position);
}
if (syncRot == true) {
stream.SendNext (transform.rotation);
}
} else { // If we are the one receiving not sending data
// In this case receive the other players info / position
if (syncPos == true) {
realPosition = (Vector3)stream.ReceiveNext ();
}
if (syncRot == true) {
realRotation = (Quaternion)stream.ReceiveNext ();
}
}
}

}


I would like to question if this is even the best way to be tracking objects in the first place or if there's something better I could be doing. (Lerping the info so it looks smooth). Any and all feedback would be greatly appreciated.

(I put the update speed for the lerp to a variable because certain things don't need to be updated often, but for most things I have it set to 0.05f)

Comments

  • Bump - This is a rather important problem that has halted a lot of development on my game, I have tried attacking this problem as many ways as I can but I can't seem to figure out a solution
  • Hi,

    I assume your game logic is that anyone can get in the car right? so one player gets in drive and exit the car, someone else gets in the car and drives it.

    The best way is to change ownership, have you tried this? there is a demo "DemoChangeOwner" provided with the Unity package.

    Bye,

    Jean
  • jeanfabre said:

    Hi,

    I assume your game logic is that anyone can get in the car right? so one player gets in drive and exit the car, someone else gets in the car and drives it.

    The best way is to change ownership, have you tried this? there is a demo "DemoChangeOwner" provided with the Unity package.

    Bye,

    Jean


    I hope you're right, and you are correct in assuming anyone can enter.
    I'm somewhat baffled that I had never considered anything like this, and I hope it is that simple. I'll try it out now, and hopefully everything works out for the best.
  • kriller509
    edited September 2016
    Options
    Well, you were right! Thanks a ton, development is full throttle once again and now that this bug is fixed we move into stage 2 of development (Stage 2/3 until our first Beta! :D).

    This was all I needed to do:

    void BecomeOwner (GameObject thePlayer) {
    PhotonView pv = gameObject.GetComponent < PhotonView > ();

    PhotonView pv2 = thePlayer.GetComponent < PhotonView > ();

    pv.TransferOwnership (pv2.ownerId);
    }

    Once again, thanks a ton!

    NOTE:
    I had to put spaces at GetComponent < Photon View > (); for some odd reason when the GetComponent< and PhotonView> were touching it caused them to dissapear the < > lines entirely (Some odd feature this forum has?) idk
  • Hi,

    Be careful, you need more than this, you need to catch the ownership transfer on every computers, so you need to implement "OnOwnershipRequest" like in DemoOwnershipGui.cs from the ownership transfer demo.

    Bye,

    Jean
  • Thanks for the catch - I haven't had any issue with the way I currently set it up but I trust that you're accurate
    So I should do something like..?:

    void BecomeOwner (GameObject thePlayer) {
    PhotonView pv = gameObject.GetComponent ();

    PhotonView pv2 = thePlayer.GetComponent ();

    pv.TransferOwnership (pv2.ownerId);
    }

    [PunRPC]
    public void OnOwnershipRequest () {
    // pv.TransferOwnership again
    }

    That seems oddly repeating - I'm not sure the direction you intended for me to go here..
  • kriller509
    edited September 2016
    Options
    I feel silly - I understand what you mean now!

    void BecomeOwner (GameObject thePlayer) {
    PhotonView pv = gameObject.GetComponent ();

    PhotonView pv2 = thePlayer.GetComponent ();

    pv111 = pv;
    pv222 = pv2;

    pv.RequestOwnership ();
    }

    [PunRPC]
    public void OnOwnershipRequest () {
    pv111.TransferOwnership (pv222.ownerID);
    }

    Is that what you were referring to? Or does it not need to be a Public void? The component is enabled for every one but I think Request Ownership auto-sends to everyone even if its not public.. So does it NEED to be public? Or am I safe just making it a normal void without the [PunRPC]?
    jeanfabre said:

    Hi,

    Be careful, you need more than this, you need to catch the ownership transfer on every computers, so you need to implement "OnOwnershipRequest" like in DemoOwnershipGui.cs from the ownership transfer demo.

    Bye,

    Jean

  • The end result: No this did not work, so I guess I'll have to grab the demo again - I had deleted them because I used similar script names as they do in the demo.