Un-Parenting a Child Object via RPC. Sync issue?

roboJ
roboJ
Hi,
I have a seemingly simple problem. Basically, my game involves a ball. A single ball, throughout my experimentation I've either had it instantiated over the network when the level is loaded or already loaded locally as a part of the scene.

Anyway, when players pick up this ball, the ball becomes a child of the player object, scales down and it's colliders are disabled.

I also have an RPC function below, which will cause the player in possession of the ball to throw it in the direction he is currently facing.
In the ball script
[code2=csharp][RPC]
public void OnThrown(Vector3 dir){
Debug.Log("I'm working");
gameObject.collider.enabled = true;
transform.localScale = new Vector3(.8f,.8f,.8f);
transform.parent = null;
rigidbody.isKinematic = false;
rigidbody.AddForce(dir * 700);
}[/code2]
And called in the player's update method like so:

[code2=csharp]//Throwing the ball
if(Input.GetMouseButtonUp(1) && _hasBall && !_stunned && PhotonView.Get (this).isMine){
_hasBall = false;
StartCoroutine(Invis());
Vector3 throwDirection = new Vector3(Mathf.Sin(Mathf.Deg2Rad * transform.eulerAngles.y),0,
Mathf.Cos(Mathf.Deg2Rad * transform.eulerAngles.y));
gameObject.GetComponentInChildren<Ball>().OnThrown(throwDirection);
PhotonView.Get (GameObject.FindGameObjectWithTag("Ball")).RPC("OnThrown",PhotonTargets.Others, throwDirection);
}[/code2]

Now for the weird part, the code works just fine when I have just one player connected to the network but when I add a second, the ball object will unparent for a second, and then immediately go back to being a child of the player who threw it in the first place. My first thought was that the game was syncing data from the other client that basically said that the balls transform was still a child of the player and put it back to it's starting position but that didn't make any sense since the ball's PhotonView didn't send any info. Nevertheless I tried turning it on and have the OnSerializeMethod sync who the transforms parent was. No dice, exact same result as before.

I also thought that maybe it was a timing issue, that the players Invis() coroutine that makes sure the player's collider doesn't immediately pickup the ball again after throwing it was somehow being thrown off so I increased the time it takes before the player can pick up the ball again, still same result.

Long story short, I've tried a lot of things but I've always ended up with the same result. Any ideas? I'm still new to networkking in general so it may be that I'm missing something really obvious. I just really need another set of eyes on this

Comments

  • Hi,

    How do you know that ball is child again? Do you log this in code?
    How do you unparent ball on throwing client? It happen in OnThrown but it's called for PhotonTargets.Others only.