Standard Assets Vehicle example in multiplayer? Wheels not syncing.

So I decided to test out the new multiplayer. Having little experience with the old system, I expected it would be just as confusing to get some basics running. But it wasn’t, the new network components are awesome and almost self explanatory.

Anyway, I decided to test it out, and I guess I picked one of the harder examples to try converting to multiplayer. Getting the vehicles to spawn and each control themselves and no one else was easy. Getting each have their own camera took a few lines of code. But then I ran into a problem.

I can’t get the wheel models to appear spinning and turning for anyone. Adding a network transform on each doesn’t do anything. I have no idea why, since that’s pretty much all it took for the car itself to sync.

Network identity on the car is local authority. And I tried the same for the wheels, and all other combinations I could think of, but they just won’t work the same as the car itself.

Do the wheels use an animation to show that they’re turning? If so then you’ll have to use a NetworkAnimator.

If you make CarController a NetworkBehaviour, then do:

    public class CarController : NetworkBehaviour
    {

        [Command]
        void CmdMove(float steering, float accel, float footbrake, float handbrake)
        {
            RpcMove(steering, accel, footbrake, handbrake);
        }

        [ClientRpc]
        void RpcMove(float steering, float accel, float footbrake, float handbrake)
        {
            if (isLocalPlayer)
                return;

            Move(steering, accel, footbrake, handbrake);
        }

        int moveCount = 0;

        public void Move(float steering, float accel, float footbrake, float handbrake)
        {
            if (isLocalPlayer)
            {
                if (moveCount++ % 10 == 0)
                {

                    CmdMove(steering, accel, footbrake, handbrake);
                }
            }

This will send the inputs from the local player to other players, which will make the remote car behave better.

1 Like

No they use a script, with wheel colliders.

I was just looking into commands and rpcs in the manual, but I don’t get how it works.

I did it and it works! So thanks for that! :smile:
Its a bit delayed and choppy/jagged but it works. I have a few questions tho. I made the CarController a network behaviour, and I put that into the CarController script, just added without changing anything. And I left the CarController enabled on the clients, but disabled CarUserControl. Did I do it right? But I’m not sure what’s going on.

Whats moveCount? And Isn’t it always 0?
To fix the choppiness, do I need to interpolate the wheels through code now?

Player #1(host)

  1. Calls Move (with own input)
  2. checks if its local player, and if 10% of move count +1 is 0, which it is, in an integer I assume. But why are we checking that?
    3a, if its local player call CmdMove, and proceed with moving.
    3a. then CmdMove sends it to the server, so we can then send to other clients, because we can’t send from client to client?
    4a. then RpcMove checks if its local player, and if not, moves the other side self?.

3b. if its not just proceed with moving, which doesn’t happen because CarUserControler is disabled.

I’m finding it really hard to wrap my head around what Command and ClientRpc do, even tho I went over the manual, probably because I’m finding it hard to wrap my head around the networking too. I don’t remember much about how it was done before, I only used it once in some prototype, but I remember we were serializing network views, and for simple commands we’d just use [RPC] for methods we want to send across network.

The “if(moveCount++%10==0)” just makes the CmdMove be called every 10 frames, so not to spam the network 60 times a second. MoveCount is incremented each frame by the ++.

So the local player essentially sends their local input to the server with the Command, then the server send that input to each other client with the ClientRpc all, and that input is applied to the remote version of the original car on those remote clients. It is like the car is being driven by a player in both places, so all the extra stuff like brake lights, wheels and skid marks just work. Then the NetworkTransform keeps the positions close to being in sync.

Hm. i also made a change to CarUserControl to prevent input from being processed, but didnt disable it.

namespace UnityStandardAssets.Vehicles.Car
{
    [RequireComponent(typeof (CarController))]
    public class CarUserControl : NetworkBehaviour
    {
        private CarController m_Car; // the car controller we want to use

        private void Awake()
        {
            // get the car controller
            m_Car = GetComponent<CarController>();
        }

        private void FixedUpdate()
        {
            if (!hasAuthority)
                return;

            // pass the input to the car!
            float h = CrossPlatformInputManager.GetAxis("Horizontal");
            float v = CrossPlatformInputManager.GetAxis("Vertical");
#if !MOBILE_INPUT
            float handbrake = CrossPlatformInputManager.GetAxis("Jump");
            m_Car.Move(h, v, v, handbrake);
#else
            m_Car.Move(h, v, v, 0f);
#endif
        }
    }
}
2 Likes

Ah I think I understand it a little better now! I had no idea you can make changes like the ++ inside an if statement. :smile:

And is “hasAuthority” the “Local Player Authority” checkbox in Network Identity? I read to turn that on if we want things working right.

This is my transform and identity settings. The rotation update was horribly delayed until I set it to 10 interpolation. That makes it at least 95% correct. I’m testing on the same machine by having two instances running. Can’t sync if audio is running right like that tho. :stuck_out_tongue:
2157148--142501--upload_2015-6-13_1-2-28.png

Also can you tell me can I interpolate between the wheel updates?

Unity doesn’t seem to recognize the “moveCount”, strange.

You should declare the variable and the it will recognize it.

I just can’t get it to work.
My with the default script the host can drive, but with this adaptation neither the host or the client can drive.
I making a FPS when you can get in and out of cars but i cant solve how to resolve this problem.