- Home /
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!
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)
}
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", RPC$$anonymous$$ode.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", RPC$$anonymous$$ode.Server);
networkView.RPC("castRay", RPC$$anonymous$$ode.Server);
networkView.RPC("moveObject", RPC$$anonymous$$ode.Server);
networkView.RPC("releaseObject", RPC$$anonymous$$ode.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;
}
For parameter of a RPC, do networkView.RPC("moveObject", RPC$$anonymous$$ode.Server, parameter, parameter, parameter, parameter, etc);
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
The Rigidbody parameter for the moveObject function is not supported by RPC's so I don't know what to do
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", RPC$$anonymous$$ode.All); and stuff like that. Try to work around the errors in networking, not trying to break through the error.