• 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
Question by OscarBrooks · Aug 30, 2013 at 08:05 PM · rigidbodynetworkingserverclientnetworkview

How do I change networkView ownership of a Rigidbody by clicking on it?

Hi, Im making a Game in Unity that involves two players throwing Rigidbodies at eachother, using a Script that I found online (Similar to the Gravity Gun in HL2).

The problem is that because the RigidBody is owned by the server's networkView, the client player is unable to move/throw it. The closest I have come to an answer is **Here**. This suggests Changing the networkView on an object depending on who is using/moving it.

I am not amazing at scripting as I like to focus more on the design elements. I have tried to get this working, but i don't really understand all about RPC's and I don't know how to implement the changing of networkViews. Ideally, I'd like to set the ownership of a Rigidbody to the person who clicked on it.

If someone could tell me how to go about doing this, it'd be very much appreciated! Please!

Comment

People who like this

0 Show 0
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
Best Answer

Answer by william9518 · Aug 30, 2013 at 08:42 PM

Make an RPC call to RPCMode.Server in your player throwing script, that adds a force on the server Rigidbody. Since the NetworkView is instantiated on the host game, it should work. As long as you use RPCMode.Server so ONLY the host recieves the RPC. Something like this:

 //C#
 void throw(){
    networkView.RPC("throwRPC", RPCMode.Server);
 }
 
 [RPC]
 void throwRPC(){
    //do throwing crap (ADD FORCE BLAHBLAHBLAH)
 }

     
         //JAVASCRIPT
         function throw(){
            networkView.RPC("throwRPC", RPCMode.Server);
         }
         
         @RPC
         function throwRPC(){
            //do throwing crap (ADD FORCE BLAHBLAHBLAH)
         }
Comment

People who like this

0 Show 5 · 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 OscarBrooks · Aug 31, 2013 at 12:26 AM 0
Share

Okay so I think I'm almost there, but there is a problem, I'm not sure what to put for the Parameters for this:

networkView.RPC("moveObject", RPCMode.Server);

Here is the full script:

 private var hittedRigidbody:Rigidbody;
 private var hit:RaycastHit;
 private var distanceToGun:float=3;
 private var catchedObject:boolean=false;
 
 function Start () {
 GravityGun();
 }
 
 function GravityGun(){
 networkView.RPC("FixedUpdate", RPCMode.Server);
 networkView.RPC("castRay", RPCMode.Server);
 networkView.RPC("moveObject", RPCMode.Server);
 networkView.RPC("releaseObject", RPCMode.Server);
 }
  
 @RPC
 function FixedUpdate(){
     if (Input.GetButton("Fire1")){
         castRay();
     }else{
         if (hittedRigidbody!=null){
             releaseObject();
         }
     }
 }
 
 @RPC
 function castRay () {
     var direction:Vector3;
     if (hittedRigidbody==null){
         direction=transform.TransformDirection(Vector3.forward);
         if (Physics.Raycast(transform.position,direction,hit,1000)){
             if (hit.rigidbody && hittedRigidbody==null){
                 hittedRigidbody=hit.rigidbody;
                 hittedRigidbody.useGravity=false;
             }
         }
     }else
     if (hittedRigidbody!=null){
         var distance:float=Vector3.Distance(hittedRigidbody.transform.position,transform.position);
         direction=transform.TransformDirection(Vector3.forward);
         
         var myRay:Ray=new Ray(transform.position,direction);
         var _pointVector:Vector3=myRay.GetPoint(distanceToGun);
         
         moveObject(hittedRigidbody,_pointVector);
         
         
         if (distance<=distanceToGun+1){
             catchedObject=true;
         }
     }
 }
 
 
 @RPC
 function moveObject(_rigidbody:Rigidbody,_vector:Vector3){
     var finalVector:Vector3=(_vector-_rigidbody.transform.position);
     _rigidbody.transform.Translate(finalVector*Time.deltaTime*10,Space.World);
     
 }
 
 @RPC
 function releaseObject(){
     var direction:Vector3=transform.TransformDirection(Vector3.forward);
     if(catchedObject)hittedRigidbody.AddForce(2000*direction);
     hittedRigidbody.useGravity=true;
     hittedRigidbody=null;
     catchedObject=false;
 }
avatar image william9518 · Aug 31, 2013 at 12:40 AM 0
Share

For parameter of a RPC, do networkView.RPC("moveObject", RPCMode.Server, parameter, parameter, parameter, parameter, etc);

avatar image OscarBrooks · Aug 31, 2013 at 12:50 AM 0
Share

I understand, but the problem is that i don't know which parameters to put in and how to format them. Sorry if I'm being stupid or something :P

avatar image OscarBrooks · Aug 31, 2013 at 12:59 PM 0
Share

The Rigidbody parameter for the moveObject function is not supported by RPC's so I don't know what to do

avatar image william9518 · Sep 02, 2013 at 01:12 AM 0
Share

Yes, you can only have RPC parameters for int, char, String, etc. You'd have to add an ID system to the moveObject method OR make it specific for one type of throwing(e.g ball) and add a separate script to the ball and say ball.networkView.RPC("BallThrow", RPCMode.All); and stuff like that. Try to work around the errors in networking, not trying to break through the error.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

18 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

Related Questions

Networking: RPC To Specific Player? 1 Answer

Unity networking tutorial? 6 Answers

Networking between projects: how to send ViewIDs? 1 Answer

Unity Networking - Clients scattered around scenes 0 Answers

Example of a non client gameobject spawning another gameobject on the server 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