Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 1234filip · Aug 14, 2018 at 08:33 AM · movementmultiplayer

Command not being called on client

So the player move command isn't being called. The loop is being called and I checked that it is only on the local player. But the actual command isn't being called. The script is on the player prefab which is registered in the network manager. What am I doing wrong?

 using UnityEngine;
 using UnityEngine.Networking;
 
 public class MovePlayer : NetworkBehaviour {
     float speed;
     Rigidbody2D rigidbody;
     Player player;
     int verticalDir = 0;
     float rot_z;
     int horizontalDir = 0;
 
     void Start () {
         player = gameObject.GetComponent<Player>();
         rigidbody = gameObject.GetComponent<Rigidbody2D>();
         speed = player.moveSpeed;
     }
 
     void Update () {
         if (!player.dead && isLocalPlayer)
         {
             // Key Movement
             verticalDir = 0;
             horizontalDir = 0;
             if (Input.GetKey("w"))
             {
                 verticalDir += 1;
             }
             if (Input.GetKey("s"))
             {
                 verticalDir -= 1;
             }
             if (Input.GetKey("a"))
             {
                 horizontalDir -= 1;
             }
             if (Input.GetKey("d"))
             {
                 horizontalDir += 1;
             }
 
             // Mouse Tracking
             Vector3 diff = player.camera.ScreenToWorldPoint(Input.mousePosition) - transform.position;
             diff.Normalize();
 
             rot_z = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg;
             CmdMove();
 
             // Velocity limit
             if (rigidbody.velocity.x > speed)
             {
                 rigidbody.velocity = new Vector2(speed, rigidbody.velocity.y);
             }
             if (rigidbody.velocity.y > speed)
             {
                 rigidbody.velocity = new Vector2(rigidbody.velocity.x, speed);
             }
         }
     }
     [Command]
     void CmdMove()
     {
         var locVel = new Vector2(speed * horizontalDir, speed * verticalDir);
         rigidbody.velocity = locVel;
         gameObject.transform.rotation = Quaternion.Euler(0f, 0f, rot_z - 90);
     }
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Zarenityx · Aug 14, 2018 at 08:27 PM

It's quite likely that the command is being called, but you don't actually see it since none of the data you modify in it actually gets sent to the clients. Commands are called on the server, and so the server's version of the object had these values changed, but the clients never saw it. What's needed is a SyncVar or a ClientRPC. RPCs are called on the client, and can be called in a command function. This causes the Rpc function to be called on every client. What you need is something along the lines of:

 [Command]
 void CmdMove(Vector2 movedir, float rotz)
 {
      RpcMove(movedir, rotz);
 }
 
 [ClientRPC]
 void RpcMove(Vector2 movedir, float rotz){
      var locVel = movedir*speed;
      rigidbody.velocity = locVel;
      gameObject.transform.rotation = Quaternion.Euler(0f, 0f, rotz - 90);
 }

(Code above is untested, but the general idea is that you need to be calling an RPC).

Note 2 things about the above code

  • The Command tells the server to call the RPC on all the clients

  • The movement vectors are passed in as parameters. The movement vector isn't synchronized and is only accurate on the client doing the moving. For all other clients and for the server this is zero, so we need to tell the server what our move vector is.



Alternately, the [SyncVar] attribute causes a variable to get sent to all clients whenever it is changed on the server, so the RPC step gets done for you.

Always remember that [Command], [ClientRPC], [TargetRPC], and [SyncVar] are just ways of hiding the messages that are being sent internally, and none of the other clients will know what your client knows until you tell the server and the server tells them.

Comment
Add comment · Share
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

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.

Follow this Question

Answers Answers and Comments

194 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 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

How would I continue an object's movement in the same direction as per last user input? 1 Answer

How to handle movement in a multiplayer FPS? 0 Answers

Can I change movement from Unity 3d into Unity 3D iOS 2 Answers

Unity networking tutorial? 6 Answers

How to make your camera move to the middle of the player photon c#? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges