Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [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;
- private PhotonView pView;
- // 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;
- pView = carriedObject.GetComponent<PhotonView>();
- pView.viewID = PhotonNetwork.AllocateViewID();
- 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;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement