- Home /
Rigidbody that two players can move/interact with over network?
2 Players. 1 is the Server, 1 is the Client. I need both players to be able to move Rigidbodies owned by the Server using the script below. Both players can move the Rigidbody, but the Server does not observe when the client moves the RigidBody because it is owned by the server. If someone could try and explain clearly what to do e.g. Not just "Use this function, make this RPC call etc..." but explain how I would write the script and what to attach the script(s) to. Thanks.
This is the script I referred to above (I attach this script to the camera of each player):
private var hittedRigidbody:Rigidbody;
private var hit:RaycastHit;
private var distanceToGun:float=3;
private var catchedObject:boolean=false;
function Start () {
}
function FixedUpdate(){
if (Input.GetButton("Fire1")){
castRay();
}else{
if (hittedRigidbody!=null){
releaseObject();
}
}
}
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;
}
}
}
function moveObject(_rigidbody:Rigidbody,_vector:Vector3){
var finalVector:Vector3=(_vector-_rigidbody.transform.position);
_rigidbody.transform.Translate(finalVector*Time.deltaTime*10,Space.World);
}
function releaseObject(){
var direction:Vector3=transform.TransformDirection(Vector3.forward);
if(catchedObject)hittedRigidbody.AddForce(2000*direction);
hittedRigidbody.useGravity=true;
hittedRigidbody=null;
catchedObject=false;
}
Answer by luckruns0ut · Aug 31, 2013 at 04:11 PM
Well what I would do is send a procedure call to the server which basically says "Hey, I want to interact with this object" and the server will perform the force to the object, and reply to the client with a response. This may cause a couple of ms lag, but it will be useful in the long run, because the server will be in complete control of the object.
If you know how to approach it, there is another trick you can do. The client can send that information to the server, like "apply this force at this point" and at the same time the client does that to the ball on his screen. Once the server gets this command, the server will perform this force and send the result to the client, e.g it will send the ball's angular velocity, and position, velocity etc to the client, and it will update on the client's screen if it was incorrect. It could cause a "rubberbanding" effect, but at the same time the client may not actually see the lag as it happens.
I like to have the server in complete control with this sort of thing, which will keep things synchronized.
Just some ideas.