Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Skumtron · Jun 08, 2014 at 10:00 AM · networkingnetworkrpc

RPC call ending up on wrong target

I hitted a speed-bump with Unity Networking. I am trying to implement a simple mechanic that damages player when he is close to an NPC.

Problem is that when I have server and client open, the server client is taking the damage IF it's been to vicinity of some NPC not the appropriate client.

Here is the relevant code inside NPCController attached to every NPC:

 void DamageTarget()
 {
     if(Network.isServer)
     {
         networkView.RPC("GiveDamage", RPCMode.All, damage);
     }
 }

 [RPC]
 void GiveDamage(int damage)
 {
     currentTarget.GetComponent<PlayerController>().TakeDamage(damage);
 }




currentTarget is set on server side after checking vicinity of NPCs:

 void CheckTargets()
 {
     // Do the initial check for colliders within range
     Collider[] hitColliders = Physics.OverlapSphere(transform.position, visibilityRadius);
     for(int i = 0; i < hitColliders.Length; i++)
     {
         if(hitColliders[i].gameObject.CompareTag("Player"))
         {
             Debug.Log("Detected a player");
             currentTarget = hitColliders[i].gameObject;
         }
     }
 }


After hours of messing around, here's what I think it should be doing:

  1. NPC objects on server keep track of their vicinity and if any player is found, it sets it as current target for that NPC.

  2. If the player is in vicinity, the NPC controlled by server calls RPC function on the client that finds the currentTarget and calls it's TakeDamage function.

What am I doing wrong?

After giving it some more thought, I feel like I'm approaching it completely wrong and should handle everything, including keeping track of the health, server side and then just update the health on Player object with RPC call. What is the proper way to do such things?

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

Answer by 34638a · Jul 09, 2014 at 11:45 AM

If what I assume about your problem is correct your NPC's are looking for a player and if they find one they see one they are just broadcasting a message that says "Hey I found a player and you should do something" and no-one is listening.

The way you need to send the RPC is through a object that is on both the client and the server. we'll call this object ClientManager. On this object you need to have the scripts that the client will use to interact with the server, from here you have a Object to interact with the NPC's. Again call it NPCManager. This object receives commands from the NPC's to broadcast to the players, when it receives a command it send a int, string, boolean value, etc. to the ClientManager which in turn sends RPC's to the clients with the information you want to send.

by setting it up so there is a individual object to send and receive commands from the server to the clients, you can more easily interact between the server, NPC objects and the client players. all you need to do with the system above is set flags to check if that player is the intended recipient of the command.

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

22 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

Related Questions

RPC setName() not working correctly 0 Answers

Networking how to correctly respawn players 2 Answers

unity c# RPC 0 Answers

View ID AllocatedID: 50 not found during lookup. 0 Answers

View ID AllocatedID: # not found during lookup. Strange behaviour may occur 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