Heya,
Been trying to get this to work for two days now. It’s my first multiplayer game so it’s a bit of a learning curve.
Currently I have it setup so that players can create a room and join. Players can move and their cameras are all correct. The issue i’m having is with the balls. The game is essentially a dodgeball game but the balls are not synching up at all. The balls can be picked up and then thrown which will cause a knockback on hit; however when a player picks up a ball, for the other players the ball does not move, once thrown the ball goes off in a direction for one player and a different direction for the others. I’ve used photon view and photon rigidbody on the balls to try to track their movement and update the server but it seems to be getting it completely wrong. I’ve also tried transform view and I am now initializing the 4 balls at the start of the game.
Please help!
From my game object:
balls = GameObject.FindGameObjectsWithTag("pickupable");
Debug.Log(balls.Length);
if(ballSpawnPoints == null && balls.Length == 0){
ballSpawnPoints = GameObject.FindGameObjectsWithTag("BallRespawn");
for(int i = 0; i < ballSpawnPoints.Length; i++){
PhotonNetwork.Instantiate(ballPrefab.name, ballSpawnPoints[i].transform.position, Quaternion.identity, 0);
}
}
From my player controller:
void Update()
{
if(photonView.isMine){
Movement();
look();
if (Input.GetMouseButtonDown(0)){
photonView.RPC("Cast", PhotonTargets.All);
}
if(pickUp.heldObj != null){
pickUp.heldObj.GetComponent<Rigidbody>().velocity = Vector3.zero;
pickUp.heldObj.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
Debug.Log(pickUp.heldObj.GetComponent<Rigidbody>().velocity);
photonView.RPC("moveObj", PhotonTargets.All);
}
}
}
[PunRPC]
void Cast(){
Debug.Log("Casting");
pickUp.Cast();
}
[PunRPC]
void moveObj(){
Debug.Log("moving");
pickUp.MoveObject();
}
From my pickup script:
public void Cast(){
if(heldObj == null){
RaycastHit hit;
if(Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, pickUpRange) && hit.transform.tag=="pickupable"){
heldObj = hit.transform.gameObject;
PickupObject(hit.transform.gameObject);
}
}
else{
DropObject();
}
}
public void MoveObject(){
if(Vector3.Distance(heldObj.transform.position, holdParent.position) > 0.1f){
Vector3 moveDirection = (holdParent.transform.position - heldObj.transform.position);
heldObj.GetComponent<Rigidbody>().AddForce(moveDirection *moveForce);
}
}
private void PickupObject(GameObject pickObj){
if(pickObj.GetComponent<SphereCollider>()){
pickObj.GetComponent<SphereCollider>().enabled = false;
}
if(pickObj.GetComponent<Rigidbody>()){
Rigidbody objRig = pickObj.GetComponent<Rigidbody>();
objRig.useGravity = false;
objRig.drag = 10;
objRig.transform.parent = holdParent;
pickObj.transform.position = holdParent.transform.position;
objRig.velocity = Vector3.zero;
objRig.angularVelocity = Vector3.zero;
heldObj = pickObj;
}
}
private void DropObject(){
if(heldObj.GetComponent<SphereCollider>()){
heldObj.GetComponent<SphereCollider>().enabled = true;
}
Rigidbody heldRig = heldObj.GetComponent<Rigidbody>();
heldRig.useGravity = true;
heldRig.drag = 0;
heldObj.transform.parent = null;
heldObj = null;
heldRig.AddForce(transform.forward * ThrowForce);
}
}
Thanks !