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 /
  • Help Room /
avatar image
0
Question by TwoTen · Jun 09, 2016 at 03:00 PM · rpcdamagecommand

Only host can damage players.

So my problem here is that my script health that manages my health and damage is not working properly. Upon entering the game the host can kill all clients. But the clients can not damage each other and the clients can not damage the host. After some searching on google i've come to the conclusion that something is wrong in this part of the script:

 function Damage(damage : float)
 {
 if (isServer)
 {
 RpcDamage(damage);
 }
 else
 {
 CmdDamage(damage);
 }
 }
 
 
 @Command
 function CmdDamage(damage : float)
 {
     Damage(damage);
 }
 
 @ClientRpc
 function RpcDamage(damage : float)
 {
 health -= damage;
 }
 
 }

Keep in mind it's written in UnityScript / JavaScript. I'm getting no errors regarding it.

Comment
Add comment · Show 17
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 TwoTen · Jun 11, 2016 at 06:43 AM 0
Share

I have tried even more and I cannot get it figured out. It seems to be just very very odd..

avatar image TwoTen TwoTen · Jun 14, 2016 at 08:28 AM 0
Share

Anyone got a solution?

avatar image TwoTen · Jun 18, 2016 at 02:38 PM 0
Share

I still have not found any answer to this problem... Does nobody know how to solve it?

avatar image mwnDK1402 TwoTen · Jun 18, 2016 at 03:10 PM 0
Share

I don't know much about networking, but I'm in the middle of my own little online project, so I'm looking for a solution to your problem. I imagine that RpcDamage isn't called on the server, so to fix that problem, you'd just write:

 @Command
  function CmdDamage(damage : float)
  {
      health -= damage;
      Damage(damage);
  }

But of course I'm not sure.

avatar image TwoTen mwnDK1402 · Jun 18, 2016 at 03:13 PM 0
Share

I will try-

Show more comments
avatar image mwnDK1402 · Jun 20, 2016 at 12:49 AM 0
Share

Why don't you use a SyncVar to track the health?

 @Command
 function CmdDamage(damage : float)
 {
      health -= damage;
 }
 
 @SyncVar
 var health : float;
avatar image TwoTen mwnDK1402 · Jun 20, 2016 at 12:51 AM 0
Share

I do use syncvar. It's just not being synced properly. The health variable does have the syncvar tag yet it's not properly being damaged.

avatar image mwnDK1402 TwoTen · Jun 20, 2016 at 01:45 AM 0
Share

What I've written is all that's nececssary to make it work...

 function Damage(damage : float)
 {
      if (!isServer)
      {
           return;
      }
      
      health -= damage;
 }

Show more comments
avatar image TwoTen mwnDK1402 · Jun 20, 2016 at 01:49 AM 0
Share

I have a seperate script on the same gameobject (the player prefab). called Weapon. so the weapon script has a OnCollisionEnter and is being called as follows:

   collision.gameObject.Send$$anonymous$$essage ("Damage", damage,Send$$anonymous$$essageOptions.DontRequireReceiver);

If you want the full script I can send that. So that is how it's being sent over to the health script wich is on the same object and has the exact code you wrote above:

  function Damage(damage : float)
  {
       if (!isServer)
       {
            return;
       }
       
       health -= damage;
  }
avatar image Munchy2007 TwoTen · Jun 21, 2016 at 11:25 AM 0
Share

Can you put a Debug.Log line in the Damage function to see if it's actually executing?

Show more comments
avatar image Munchy2007 · Jun 21, 2016 at 01:27 PM 0
Share

It sounds like you're probably trying to run a Command on the client, which is why it only works one way around.

I can't help with actual code as I don't know UnityScript, but the basic process needs to work like this:-

Get a reference to the GameObject you hit.

Then use a Command function to call a 'takedamage' function on the hit GameObject (the client can't call the function directly, which is why it only works one way on your tests).

The 'takedamage' function should be marked with the [server] attribute, so it only runs on the server copy of the script, and it should be in a script that also contains the health syncvar. It can then modify the health value which will then propagate its new value to all other clients and run any code contained in the hook function.

$$anonymous$$y UNET tutorial has a simple example of how to go about this http://www.doofah.com/tutorials/networking/health-and-damage/

The most common reason for the kind of problem you are encountering is due to clients trying to directly execute code that needs to be executed on the server.

avatar image TwoTen Munchy2007 · Jun 21, 2016 at 01:31 PM 0
Share

How would you code it in C#? Could you provide me with an example so I can understand it easier?

avatar image Munchy2007 TwoTen · Jun 21, 2016 at 04:01 PM 0
Share

See my comment above, I provided a link to my UNET tutorial which provides about as simple an example as you can get of how to handle it in C#.

To answer your comment below, it depends on how you have coded your attack function, but in my tutorial I use a raycast to get a reference to the gameobject, however you could just as easily use collisions.

This would probably be better dealt with in the community forum than here.

avatar image TwoTen Munchy2007 · Jun 21, 2016 at 01:44 PM 0
Share

Also, how do I make a reference to the gameobject I hit? Any C# Examples?

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

RPC Not working 0 Answers

Question to RPC and Command (Unet) 0 Answers

ClientRpc/Commands between two seperate projects? 0 Answers

Damage Players Using Raycast 0 Answers

How to update player from another player in Local Player Authority 0 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