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 Devastus · Jul 09, 2016 at 02:35 PM · networkinginterfaceattribute

Problem with interfaces and Unity Networking Attributes

Alright, so there is either a great flaw in my understanding of interface implementation regarding attributes in general (which is most likely), or there is a bug within UNet, and I am seeking some clarification on that.

What I want to do is to be able to have an interface called IDamageable describe general functionality between objects that can be damaged. This would contain not only players themselves, but some props in the scene as well.

I went on to achieve this with polymorphism / deriving classes after I encountered this problem of mine, but it's probably pretty clear why an interface would be a better option (ie. player is not a table, table is not a car, but all of them can be damaged).

So, here are some simplified bits of the attack mechanics for demonstration:

 //Attack something, call this from mouse-click, animation event etc.
     [Client]
     void Attack()
     {
         if (isLocalPlayer)
         {
             Ray ray = new Ray(cam.position, cam.forward);
             RaycastHit hit;
             if (Physics.Raycast(ray, out hit, attackRange, hitBoxMask))
             {
                 CmdCheckHit(hit.transform.root.name, ray, damage);
             }
         }
     }

Then there is the command:

 //Process this on the server
     [Command]
     void CmdCheckHit(string id, Ray r, int damage)
     {
         //This is inefficient, but just to demonstrate
         IDamageable entity = GameObject.Find(id).GetComponent<IDamageable>();
         entity.RpcTakeDamage(r, damage);
     }

Everything is fine at this point, command gets sent and processed in the server, we find the component that implements IDamageable and call the implemented function. Now that brings up the problem:

 //This should run not on the server, but on all the clients
     [ClientRpc]
     public void RpcTakeDamage(Ray r, int damage)
     {
         Debug.Log(transform.name + " was hit for " + damage + " damage, from position " + r.origin + " and direction " + r.direction);
     }

This is a function implemented from the interface IDamageable. Regardless of whether or not I have "using UnityEngine.Networking" and the attribute [ClientRpc] in the interface itself (which both, in my understanding, would not be required there anyway), this will run only on the server (or the host client) and not on the remote clients. That means it does not recognize the attribute.

This may happen because the interface is not a NetworkBehaviour (which it of course cannot be), although I don't really know why that would mean anything because it is an interface, not a class.

Just to be clear, the interface would be quite simply just like this:

 public interface IDamageable {
 void RpcTakeDamage(Ray r, int damage);
 }

So the question is: is this behaviour intended? If it is, I'd like to understand the reasoning behind it. Or is something amiss? Also, hearing possible workarounds other than my current one (I have a NetworkEntity class that everything damageable derives from, which is not necessarily ideal) would be very welcome .

EDIT: I'll add that while a simple health reduction etc. could be done via a SyncVar change (at which point all this fuss would be insignificant), the problem at hand involves being able to process the reaction of getting hit on the clients themselves for advanced, unique outcomes (such as physics calculations and such).

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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Implementing Unity's Callback System 1 Answer

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

TargetRpc not sending atribute (Mirror) 0 Answers

Calling Command Function from Client results in "NetworkServer not active..." 1 Answer

Unity networking tutorial? 6 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