- Home /
How to make script work for all players Unity Photon?
Prelude: Again i`m asking about moving my stupid stones and boxes, but after almost 2 months of work, i am still unable to work this out. (I`ve been learning unity for 3 months total).
I want players to be able to move and throw gameObjects in the scene using this script:
[RequireComponent( typeof( PhotonView ) )]
public class PickupObjectNetwork : Photon.MonoBehaviour {
public GameObject mainCamera;
public bool carrying;
GameObject carriedObject;
public float distance;
public float smooth;
public float throwPower = 10f;
private float chargeTime = 0f;
// Use this for initialization
void Start () {
mainCamera = GameObject.FindWithTag("MainCamera");
}
// Update is called once per frame
void Update () {
if(photonView.isMine)
{
if(carriedObject == null)
{
carrying = false;
}
if(gameObject == null)
return;
if(carrying) {
photonView.RPC("carry", PhotonTargets.AllBuffered);
photonView.RPC("checkDrop", PhotonTargets.AllBuffered);
if (Input.GetMouseButtonUp (0)) {
float pushForce = chargeTime * 10;
pushForce = Mathf.Clamp (pushForce, 1, 20);
photonView.RPC("throwObject", PhotonTargets.AllBuffered, pushForce);
chargeTime = 0;
}
} else {
if(Input.GetKeyDown (KeyCode.E)) {
photonView.RPC("pickup", PhotonTargets.AllBuffered);
}
}
if(Input.GetMouseButton(0)){
chargeTime += Time.deltaTime;
}
}
}
[PunRPC]
void carry() {
if (carriedObject == null)
{
return;
}
Debug.Log("picked up object with id: " + carriedObject.GetComponent<PhotonView>().viewID);
carriedObject.transform.position = Vector3.Lerp (carriedObject.transform.position, mainCamera.transform.position + mainCamera.transform.forward * distance, Time.deltaTime * smooth);
carriedObject.transform.rotation = Quaternion.identity;
}
[PunRPC]
void pickup() {
int x = Screen.width / 2;
int y = Screen.height / 2;
Ray ray = mainCamera.GetComponent<Camera>().ScreenPointToRay(new Vector3(x,y));
RaycastHit hit;
if(Physics.Raycast(ray, out hit)) {
Pickupable p = hit.collider.GetComponent<Pickupable>();
if(p != null && hit.distance < 5) {
carrying = true;
carriedObject = p.gameObject;
p.gameObject.GetComponent<Rigidbody>().velocity = Vector3.zero;
p.gameObject.GetComponent<Rigidbody>().useGravity = false;
}
}
}
[PunRPC]
void checkDrop() {
if(Input.GetKeyDown (KeyCode.E)) {
dropObject();
}
}
[PunRPC]
void dropObject() {
carrying = false;
carriedObject.gameObject.GetComponent<Rigidbody>().useGravity = true;
carriedObject = null;
}
[PunRPC]
void throwObject(float pushForce){
carrying = false;
if(carriedObject.tag != "BoxMultiplayer")
carriedObject.tag = "Projectile";
carriedObject.GetComponent<Rigidbody> ().AddForce(Camera.main.transform.forward * pushForce * 200);
carriedObject.gameObject.GetComponent<Rigidbody> ().useGravity = true;
carriedObject = null;
}
}
I instantiate scene objects using PhotonNetwork.InstantiateSceneObject(); This script works only for master client, non-master clients can`t move objects. I also tried this script (randomly):
http://pastebin.com/7DgjZ9U7 (had to put it in pastebin, because it hadn`t been displayed correctly for some reason)
I don`t really know how to work with AllocateViewId(). When i use this script, both players can move sceneObjects, but object movement is not synchronized (object positions are not updating for other players). Also, when one of the players destroy gameObject, by throwing it into the wall, which destroys new collisions with PhotonNetwork.Destroy(), this player freezes in place for other players.
I know i`m asking a lot of questions, but i don`t see other variants how to finish my project
also i don`t know how to avoid calling RPC`s in the Update function in this script, i know that this is bad
Did you figure this out? Because it's my problem too.