• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by qiterpot · Aug 07, 2020 at 08:26 AM · networking

Unity Networking UNET: Client can't send commands to Server,Unity Networking: Client Can't send commands to server

Hey I've been working a game and I cannot get my client to send anything to the server. Basically, you're suppose to be able to control little turtle shells with your mouse and control a little hamburger with WASD. The main thing I'm trying to get working is the hamburger that both the host and the client can individually control their own copies of. Currenty, the Host can control the burger and the client can see all changes. But no matter what, I cannot get the server to respond to any commands from the client, even with client authority. Here is my code for the network spawning:

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking;

public class NetworkingSpawnCode : NetworkBehaviour { public GameObject Burger; public GameObject follow; public GameObject enemy; public GameObject enemycontroller; private float movementSpeed = 5f; GameObject myPlayerUnit;

 // Use this for initialization
 void Start () {
     if (isLocalPlayer == false) {
         return;
         //    

     }
     CmdSpawns ();

 }
 
 // Update is called once per frame
 void Update () {
     if (isLocalPlayer == false) {
         return;
         //    

     }
     if (Input.GetKey ("e")) {
         myPlayerUnit.transform.Translate (1, 1, Time.deltaTime);
     }

     //CmdMoveBurger ();
 }

 [Command]
 void CmdMoveBurger()
 {
     

     
     Vector3 p = myPlayerUnit.transform.position;
     //get the Input from Horizontal axis
     float horizontalInput = Input.GetAxis ("Vertical");
     //get the Input from Vertical axis
     float verticalInput = Input.GetAxis ("Horizontal");

     //update the position
     myPlayerUnit.transform.position = myPlayerUnit.transform.position + new Vector3 (horizontalInput * movementSpeed * Time.deltaTime, 0, verticalInput * movementSpeed * Time.deltaTime);

}

 [Command]

 void CmdSpawns()
 {
     
 //    NetworkServer.SetClientReady (connectionToClient);
     GameObject go = (GameObject)Instantiate(Burger, transform.position + new Vector3(-19, 2, 20), Quaternion.identity);

     myPlayerUnit = go;

     NetworkServer.SpawnWithClientAuthority(go, connectionToClient);
     //GameObject go2 = (GameObject)Instantiate(follow, transform.position + new Vector3(-30, 2, 20), Quaternion.identity);
     //GameObject go3 = (GameObject)Instantiate(enemy, transform.position + new Vector3(-35, 2, 20), Quaternion.identity);
     GameObject go4 = (GameObject)Instantiate(enemycontroller, transform.position + new Vector3(-19, 2, 20), Quaternion.identity);

     //go.GetComponent<NetworkIdentity> ().AssignClientAuthority (connectionToClient);

     //NetworkServer.Spawn (go2);
     //NetworkServer.Spawn (go3);
     NetworkServer.Spawn (go4);

     CmdMoveBurger ();

 }

}


Now here is the code for the hamburger movement"

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking;

// A PlayerUnit is a unit controlled by a player // This could be a character in an FPS, a zergling in a RTS // Or a scout in a TBS

public class PlayerUnit : NetworkBehaviour {

 // Use this for initialization
 void Start () {

 }

 Vector3 velocity;

 // The position we think is most correct for this player.
 //   NOTE: if we are the authority, then this will be
 //   exactly the same as transform.position
 Vector3 bestGuessPosition;

 // This is a constantly updated value about our latency to the server
 // i.e. how many second it takes for us to receive a one-way message
 // TODO: This should probably be something we get from the PlayerConnectionObject
 float ourLatency;   

 // This higher this value, the faster our local position will match the best guess position
 float latencySmoothingFactor = 10;

 // Update is called once per frame
 void Update () {
     // This function runs on ALL PlayerUnits -- not just the ones that I own.

     // Code running right here is running for ALL version of this object, even
     // if it's not the authoratitive copy.
     // But even if we're NOT the owner, we are trying to PREDICT where the object
     // should be right now, based on the last velocity update.


     // How do I verify that I am allowed to mess around with this object?
     if( hasAuthority == false )
     {
         // We aren't the authority for this object, but we still need to update
         // our local position for this object based on our best guess of where
         // it probably is on the owning player's screen.

         bestGuessPosition = bestGuessPosition + ( velocity * Time.deltaTime );

         // Instead of TELEPORTING our position to the best guess's position, we
         // can smoothly lerp to it.

         transform.position = Vector3.Lerp( transform.position, bestGuessPosition, Time.deltaTime * latencySmoothingFactor);

         return;
     }


     // If we get to here, we are the authoritative owner of this object
     transform.Translate( velocity * Time.deltaTime );


     if( Input.GetKeyDown(KeyCode.Space) )
     {
         this.transform.Translate( 0, 1, 0 );
     }

     if(Input.GetKeyDown(KeyCode.D))
     {
         Destroy(gameObject);
     }

     if( /* some input */ true )
     {
         // The player is asking us to change our direction/speed (i.e. velocity)

         velocity = new Vector3(1, 0, 0);

         CmdUpdateVelocity(velocity, transform.position);
     }

 }

 [Command]
 void CmdUpdateVelocity( Vector3 v, Vector3 p)
 {
     // I am on a server
     transform.position = p;
     velocity = v;

     // If we know what our current latency is, we could do something like this:
     //  transform.position = p + (v * (thisPlayersLatencyToServer))


     // Now let the clients know the correct position of this object.
     RpcUpdateVelocity( velocity, transform.position);
 }

 [ClientRpc]
 void RpcUpdateVelocity( Vector3 v, Vector3 p )
 {
     // I am on a client

     if( hasAuthority )
     {
         // Hey, this is my own object. I "should" already have the most accurate
         // position/velocity (possibly more "Accurate") than the server
         // Depending on the game, I MIGHT want to change to patch this info
         // from the server, even though that might look a little wonky to the user.

         // Let's assume for now that we're just going to ignore the message from the server.
         return;
     }

     // I am a non-authoratative client, so I definitely need to listen to the server.

     // If we know what our current latency is, we could do something like this:
     //  transform.position = p + (v * (ourLatency))

     //transform.position = p;

     velocity = v;
     bestGuessPosition = p + (velocity * (ourLatency));


     // Now position of player one is as close as possible on all player's screens

     // IN FACT, we don't want to directly update transform.position, because then 
     // players will keep teleporting/blinking as the updates come in. It looks dumb.


 }

}

I've tried checking other people with similar problems but I just could not replicate their solutions at all. There is something basic I'm missing and I just cant tell what it is. Been trying to get this to work for days now. I know Unet is dead but I really wanted to get this to work. If anyone still remembers how to program with it, please help me!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

182 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity networking tutorial? 6 Answers

Photon Unity Networking Score Issue 1 Answer

Animating over a network not working 0 Answers

[Unet] Network Animation and Bone Rotation using RPC 0 Answers

zombie network spawn! help" 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges