Advertisement
Guest User

Untitled

a guest
Nov 19th, 2016
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. [RequireComponent( typeof( PhotonView ) )]
  2. public class PickupObjectNetwork : Photon.MonoBehaviour {
  3. public GameObject mainCamera;
  4. public bool carrying;
  5. GameObject carriedObject;
  6. public float distance;
  7. public float smooth;
  8. public float throwPower = 10f;
  9. private float chargeTime = 0f;
  10. private PhotonView pView;
  11. // Use this for initialization
  12. void Start () {
  13. mainCamera = GameObject.FindWithTag("MainCamera");
  14. }
  15.  
  16. // Update is called once per frame
  17. void Update () {
  18. if(photonView.isMine)
  19. {
  20. if(carriedObject == null)
  21. {
  22. carrying = false;
  23. }
  24. if(gameObject == null)
  25. return;
  26.  
  27. if(carrying) {
  28. photonView.RPC("carry", PhotonTargets.AllBuffered);
  29. photonView.RPC("checkDrop", PhotonTargets.AllBuffered);
  30.  
  31. if (Input.GetMouseButtonUp (0)) {
  32. float pushForce = chargeTime * 10;
  33. pushForce = Mathf.Clamp (pushForce, 1, 20);
  34. photonView.RPC("throwObject", PhotonTargets.AllBuffered, pushForce);
  35. chargeTime = 0;
  36. }
  37. } else {
  38. if(Input.GetKeyDown (KeyCode.E)) {
  39. photonView.RPC("pickup", PhotonTargets.AllBuffered);
  40. }
  41. }
  42.  
  43. if(Input.GetMouseButton(0)){
  44. chargeTime += Time.deltaTime;
  45. }
  46. }
  47. }
  48.  
  49. [PunRPC]
  50. void carry() {
  51. if (carriedObject == null)
  52. {
  53. return;
  54. }
  55. Debug.Log("picked up object with id: " + carriedObject.GetComponent<PhotonView>().viewID);
  56. carriedObject.transform.position = Vector3.Lerp (carriedObject.transform.position, mainCamera.transform.position + mainCamera.transform.forward * distance, Time.deltaTime * smooth);
  57. carriedObject.transform.rotation = Quaternion.identity;
  58. }
  59.  
  60. [PunRPC]
  61. void pickup() {
  62. int x = Screen.width / 2;
  63. int y = Screen.height / 2;
  64.  
  65. Ray ray = mainCamera.GetComponent<Camera>().ScreenPointToRay(new Vector3(x,y));
  66. RaycastHit hit;
  67. if(Physics.Raycast(ray, out hit)) {
  68. Pickupable p = hit.collider.GetComponent<Pickupable>();
  69. if(p != null && hit.distance < 5) {
  70. carrying = true;
  71. carriedObject = p.gameObject;
  72. pView = carriedObject.GetComponent<PhotonView>();
  73. pView.viewID = PhotonNetwork.AllocateViewID();
  74. p.gameObject.GetComponent<Rigidbody>().velocity = Vector3.zero;
  75. p.gameObject.GetComponent<Rigidbody>().useGravity = false;
  76. }
  77. }
  78. }
  79. [PunRPC]
  80. void checkDrop() {
  81. if(Input.GetKeyDown (KeyCode.E)) {
  82. dropObject();
  83. }
  84. }
  85. [PunRPC]
  86. void dropObject() {
  87. carrying = false;
  88. carriedObject.gameObject.GetComponent<Rigidbody>().useGravity = true;
  89. carriedObject = null;
  90. }
  91. [PunRPC]
  92. void throwObject(float pushForce){
  93. carrying = false;
  94. if(carriedObject.tag != "BoxMultiplayer")
  95. carriedObject.tag = "Projectile";
  96. carriedObject.GetComponent<Rigidbody> ().AddForce(Camera.main.transform.forward * pushForce * 200);
  97. carriedObject.gameObject.GetComponent<Rigidbody> ().useGravity = true;
  98. carriedObject = null;
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement