Converting single player character to multiplayer have problem with picking up ball

Options
Just to let everyone know I just started with photon, but I've watch tutorials and read a lot of discussion(mostly old) about item pickup. I'm trying to make a dodge ball game where the player can pick up the ball and then the object will become the child of the player. I've tried to use the RPC function and put my single player code inside it works but only the client could see and the others see the ball still on the ground. I need help about Photon and the logic behind it.

Here is the code
Im sry this is my first time so im not sure how to paste code
[PunRPC]
void grabBall()
{
RaycastHit hit;
Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0));
rayOrigin += fpsCam.transform.forward * 1.05f;
if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out hit, pickUpBallDis))
{
//Debug.Log(hit.transform.tag);
if (hit.transform.gameObject.CompareTag("ballOnGround"))
{
//Debug.Log("Hit");
ballColliderObj = hit.collider;
ballRigidObj = hit.rigidbody;
hit.transform.SetParent(Hand.transform);
hit.transform.position = Hand.transform.position;
hit.rigidbody.constraints = RigidbodyConstraints.FreezePosition;
hit.collider.enabled = false;
haveBall = true;
}
}
}

Comments

  • BigGameCo
    Options
    I think maybe in your grabBall function shouldn't be a PunRPC, and when you detect that 'this' player has just picked it up the ball, you call an RPC on everyone that parents the ball to that player. Because won't the fpsCam raycast be different on every client?

    something like
    if (hit.transform.gameObject.CompareTag("ballOnGround"))
    {
    photonView.RPC("PickedUpBall", RpcTarget.All);
    }
    
    [PunRPC]
    void PickedUpBall()
    {
    
    ballColliderObj = hit.collider;
    ballRigidObj = hit.rigidbody;
    hit.transform.SetParent(Hand.transform);
    hit.transform.position = Hand.transform.position;
    hit.rigidbody.constraints = RigidbodyConstraints.FreezePosition;
    hit.collider.enabled = false;
    haveBall = true;
    }