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 Graithen · May 30, 2019 at 06:14 PM · c#multiplayermultiplayer-networkingenvironmentinteractable

Creating interactable enviornment items that are usable by all players in Multiplayer

I am just wondering what the best approach is to handling world items that all other players on a server can interact with. A good example would be switches and doors, that exist as part of the scene, but would need to handled by each players client as they interact with it. I've tried to wrap my head around it, but I am coming up blank. I have seen people say that the prefabs should be spawned in on launch, but that doesn't seem like it would help if the objects are static parts of the environment. Whenever I do attempt something, I get the old "Trying to send command for object without authority."

Any help would be greatly appreciated.

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

2 Replies

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

Answer by Graithen · Jun 16, 2019 at 04:03 PM

Ok! After much trial and error, I figured out a system that works fine for me that solves my initial problem. Part of my solution is handled inside the character controller, and part inside the object itself.

 if (Input.GetKeyDown(KeyCode.E))
             {
                 CmdActivateObject();
             }
 
 //A trigger volume in front of the player looks for objects with an 'interactable' tag, and when it finds them it will pass it into AssignInteractables!
 
     private void OnTriggerStay(Collider other)
     {
         if (other.gameObject.tag == "Interactable")
         {
             AssignInteractables(other.gameObject);
             Debug.Log(other.gameObject.name);
         }
     }
     private void OnTriggerExit(Collider other)
     {
         AssignInteractables(null);
     }
 
 //I use this to assign the object that will be interacted with
 
 public void AssignInteractables(GameObject interactable)
     {
         ObjectToAssign = interactable;
         if (ObjectToAssign != null) {
             Debug.Log(ObjectToAssign.name + " ready to assign!");
         }
     }
 
 
 //This next part handles the interaction and the replication of the interaction on the server and across all other clients. It will reach into the interactable object and look for the Network Interaction script and then change its 'active' state.
 
 [Command]
     void CmdActivateObject()
     {
         bool activStat = ObjectToAssign.GetComponent<NetworkInteraction>().PlayerActivated;
         activStat = !activStat;
         if(isServer)
         {
             RpcActivateObject(activStat);
         }
     }
     [ClientRpc]
     void RpcActivateObject(bool state)
     {
         ObjectToAssign.GetComponent<NetworkInteraction>().PlayerActivated = state;
     }

On each object that I want the player to be able to interact with, I just assign my 'Network Interaction' script:

 public class NetworkInteraction : MonoBehaviour
 {
     [Header ("Activation State")]
     public bool PlayerActivated;
 }

Because it is the same across all systems, I can then write any script I want, and reference this PlayerActivated boolean using a locally running script to do all the complex things I need an object to do...

It might not be the most elegant way to handle this problem, but like I said it deffo works for me!

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
avatar image
0

Answer by Bunny83 · May 30, 2019 at 07:19 PM

I guess you use UNet!?


Any object that is not owned by a single player is owned by the server and therefore only the server can control it. If you want to change the state of such an object from a client, the client has to send a command through his own player object and on the serverside the server can verify the request and forward it to the object in question.


Any communication from the client goes through his player object. In most multiplayer games you only have a few physical inputs / keys that a player can actually use. (like pressing "e" to "use" something, mouse0 to fire, ... ). There are generally two approaches:

  • Either you just send the actual input events to the server and do everything else on the serverside. On the server you can do everything you want.

  • Another option is to do some pre determining on the client and send more explicit commands to the server (again, everything through the player object). For example you can do the raycasting which determines which object a player is looking at on the client and just pass a gameobject reference as parameter to the command you send to the server. However the more you do already on the client, the more difficult it is for the server to verify the validity of the command. A similar issue you see in some games that work this way. For example minecraft. The client essentially tells the server that he interacted with a certain block. However a cheater could simply tell the server he ineracted with a block that isn't reachable by the player.


So the best security is reached by only sending input events and do the rest on the server. Best precision and user feedback is reached when you do some preprocessing on the client.


So either you just send an interact() message to the server and let the server do the rest, or you send commands like interact(GameObject) where you pass along the object you want to interact with.

Comment
Add comment · Show 1 · 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 Graithen · May 30, 2019 at 09:32 PM 0
Share

Thanks for the reply! I am indeed using UNet. I am just wondering how would I go about sending the input events to the server? When I try and run a [Command] on a client locally, it says there is an issue with the client not having authority. I completely understand having things running on the server to prevent cheating, I am just unsure how to handle getting a door script run locally with authority for all players. I had presumed I would have to change the objects authority to the client intending to use it, but now I am not so sure.

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

185 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

Related Questions

PUN - Choose random Hero for player 0 Answers

Kill System, who killed who (UNET) 1 Answer

Multiple Cars not working 1 Answer

How would you spawn UI GameObjects inside a grid in multiplayer? 2 Answers

Distribute terrain in zones 3 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