I’m using spawnwithAuthority to give players the ability to drive vehicles smoothly but I’d also like to switch them out… the only way I can see doing that now is to spawn a new vehicle when the player “gets in” (touches its trigger collider) and despawning it when he gets out, and replacing it with a server controlled one… then when the other player touches it, spawnwithAuthority replace it with that one.
I was also wondering about it. I would prefer everything to be server controlled (multiple player objects), but as for now it’s not possible - and so when my player’s ships module gets destroyed, it gets deattached, and I want player to lose control over it then - so pass authority to server.
SEAN!!! I think I found a bug with this. I just had 2 guys connect to my game and try it because I needed to make sure it is what I thought it is.
Basically… when a client “takes authority” of the car, it ROCKS… the car moves and I see it go on the host. He presses “L” to get out, and that calls “RemoveClientAuthority” … the OTHER client gets in… takes off and it ROCKS… everything is working perfect. He presses “L” and removes his authority. BUT… now “I” (the HOST) get in, and I AssignClientAuthority with my host connection and the car MOVES ON MY SCREEN but the NetworkTransform no longer updates the clients with the new position! OH NOES!!! I’m SO CLOSE MAN THIS IS KILLING ME.
Hm. setting authority to a local client connection is an interesting use case. I would not be surpised if that had not been tested.
There is also NetworkIdentity.RemoveClientAuthority() which reverts control back to the server, but that is not quite the same as assigning authority to the local client on the host.
The problem could possibly be then that I’m calling NetworkIdentity.AssignClientAuthority() with the HOST connection and that is what is making it “break”… I could try it without that call except I don’t know an easy way to “know” if I’m a host and not a client when the player object hits the cars trigger collider. In my mind it should be “ok” to call that function with the host ID though, so I appreciate you looking into it.
SWEET! I was just going to tell you that without using the “AssignClientAuthority” from the host connection the host can’t control it even though the client removed the authority.
So… on my “server spawned object” (in this case a car) I have a script that handles the trigger collision with the player… when the player collides the script calls a function on a script that is attached to the player object:
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == “Player”)
{
other.gameObject.GetComponent().DisablePlayerEnableCar();
}
}
The script SwitchControllers attached to the players, DisablePlayerEnableCar does some activating/deactivating of components but the first thing we’re interested in for this thread is here:
public void DisablePlayerEnableCar()
{
if (!isLocalPlayer)
return;
Debug.Log("IDs: " + this.GetComponent().connectionToServer.connectionId.ToString());
The command to assign that connection looks like this:
[Command]
void CmdServerAssignClient()
{
GameObject CarServer = GameObject.Find(“AssaultVehicle”);
CarServer.GetComponent().AssignClientAuthority(this.GetComponent().connectionToClient);
}
When the player presses “L” the swtichcontroller switch calls this command to remove his authority:
So now… in order to “move the car” I have a PlayerCarController script on the player object, that applies the controls and sends those values to the Controller script attached to the vehicle. Without clientauthority this had to be a server command and that caused some “lag” from clients as well as a LOT of network traffic… instead now we’re directly calling our version of the “local car” and it’s sending it’s networktransform data up the wire to the server as expected:
void Start()
{
if (isLocalPlayer)
{
Car = GameObject.Find(“AssaultVehicle”);
CarScript = Car.GetComponent();
}
}
void FixedUpdate()
{
if (!isLocalPlayer)
return;
Thanks for your valueable code, seems it is working but showing me this error:
NullReferenceException: Object reference not set to an instance of an object
UnityEngine.Networking.NetworkTransform.SendTransform () (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkTransform.cs:1103)
UnityEngine.Networking.NetworkTransform.Update () (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkTransform.cs:1031)
I don’t where is the problem as it showing in UNet HLAPI. Can you see this?