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 JaredHD · Sep 27, 2017 at 07:18 AM · networkingmultiplayerattribute

When and how to use [ClientRpc] and [Command]

Hello, so I've been looking at the unity documentation trying to understand networking, but I'm a little confused about [ClientRpc] and [Command].

Lets say I have a player with a simple move script Eg:

     [Command]
     void CmdMove()
     {
         //Do move stuff here
     }

Is using the Command right in this case, or should I use ClientRpc?

If I want to spawn an object into unity. Do I use a command to tell the server? How do the other players know it was spawned?

So my main question is when and how do I use these commands. Also would [Client] need to be joined when using this?

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
8
Best Answer

Answer by Bunny83 · Sep 27, 2017 at 08:12 AM

A method with the Command attribute is ment to be called from a client but is are actually executed on the server.

A method with the ClientRPC attribute is ment to be called from the server but are executed on all clients instead.

Keep in mind that the method is bound to the object where the NetworkBehaviour is attached to. So if Player1 sends a command "CmdMoveForward" the command is executed on the server only on the object that is accociated with Player1. Likewise when the server calls a ClientRPC methhod RpcMovePlayer on the object accociated with Player3, this method is called on all clients on the Player3 object.

So commands are generally used to tell the server what you want to do and clientrpcs are used by the server to tell all clients what actually has happened.

This general approach is called an authoritative server as the server actually determines what happens.

So roughly speaking:

  • Commands tell the server what a client wants to change.

  • ClientRpcs tell the clients what the server has decided to do.

This allows the server to check all user input (the commands it receives) and ignore invalid input.

Comment
Add comment · Show 3 · 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
avatar image JaredHD · Sep 27, 2017 at 09:02 AM 0
Share

Ok that's cleared quite a bit of my confusion, thanks.

But if I tell the server to move forward with [Command] how do other players know that the player moved. Only the server know?

avatar image PersianKiller · Jul 05, 2019 at 09:27 AM 0
Share

@Bunny83,what about [PunRPC] ?? how does it work?

avatar image Bunny83 PersianKiller · Jul 05, 2019 at 10:47 AM 0
Share

Consult the Photon documentation. Photon is a third party networking solution which I have never used. Though as far as I know it's very similar to Unity's old Raknet networking engine.

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

135 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

Related Questions

Unity networking tutorial? 6 Answers

Using Mirror Networking, is there a "Run Everywhere" attribute? 1 Answer

Animation & sound over network 1 Answer

Syncing terrain over Network 0 Answers

How do I add gravity to my object, and how do I fix my network problem? 1 Answer


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