Advertisement
Guest User

PlayerPickupScript

a guest
Apr 27th, 2014
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.91 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. // This script is attached to each player. It allows them to pick up objects.
  5.  
  6.  
  7. public class PlayerPickupScript : MonoBehaviour {
  8.    
  9.     // Declare Variables
  10.    
  11.     // The range of the cast ray
  12.     private float range = 1.5f;
  13.    
  14.     // The name of the ray cast
  15.     RaycastHit hit;
  16.    
  17.     // Is the object I'm targeting able to be picked up?
  18.     public bool canBePickedUp = false;
  19.    
  20.     // The player can only pick up an object if they aren't currently holding one
  21.     private bool areMyHandsFull = false;
  22.    
  23.     // Prevent the player from throwing an object the second it is picked up
  24.     private bool canIthrow = false;
  25.    
  26.     private float throwForce = 10;
  27.    
  28.     // References
  29.     private Transform myTransform;
  30.     private Transform myCameraHeadTransform;
  31.     private Transform itemHolder;
  32.     private GameObject heldItem;
  33.     private IdentifyPickupScript identifyPickupScript;
  34.     private PickupMovementUpdate pickupMovementUpdate;
  35.     private string playerName;
  36.  
  37.     // Use this for initialization
  38.     void Start ()
  39.     {
  40.         myTransform = transform;
  41.         myCameraHeadTransform = myTransform.Find("CameraHead");
  42.         itemHolder = myTransform.Find("ItemHolder");
  43.         identifyPickupScript = myTransform.GetComponent<IdentifyPickupScript>();
  44.         playerName = PlayerPrefs.GetString("playerName");
  45.        
  46.         /* if (networkView.isMine == true)
  47.         {
  48.             myTransform = transform;
  49.            
  50.             // The ray is cast based on the direction the camera head is facing
  51.             // Remember that the camera head tilts up and down because of
  52.             // the MouseLook script attached to it
  53.            
  54.             myCameraHeadTransform = myTransform.Find("CameraHead");
  55.             itemHolder = myTransform.Find("ItemHolder");
  56.             identifyPickupScript = myTransform.GetComponent<IdentifyPickupScript>();
  57.             playerName = PlayerPrefs.GetString("playerName");
  58.  
  59.         }
  60.        
  61.         else
  62.         {
  63.             enabled = false;
  64.         } */
  65.     }
  66.    
  67.     // Update is called once per frame
  68.     void Update ()
  69.     {
  70.         // Fire a ray from the cameraHead heading straight forward. NOTE: THIS HAPPENS In THE IdentifyPickupScript now
  71.         /* if (Physics.Raycast (myCameraHeadTransform.position, myCameraHeadTransform.forward, out hit, range))
  72.         {
  73.             if (hit.transform.tag == "Pickup")
  74.             {
  75.             canBePickedUp = true;
  76.             }
  77.         }
  78.        
  79.         if (Physics.Raycast (myCameraHeadTransform.position, myCameraHeadTransform.forward, out hit))
  80.         {
  81.             if (hit.transform.tag != "Pickup")
  82.             {
  83.             canBePickedUp = false;
  84.             }
  85.         } */
  86.    
  87.         // I'm going to continue use the Place Block input for now since this is frankensteined from the PlayerAsksServerToPlaceConstructionBlock script
  88.         if (Screen.lockCursor == true && Input.GetButtonDown("Place Block") && areMyHandsFull == false && canBePickedUp == true)
  89.         {
  90.             heldItem = identifyPickupScript.hit.transform.gameObject;
  91.             Debug.Log("Picking up: " + heldItem);
  92.             areMyHandsFull = true;
  93.             identifyPickupScript.hit.transform.position = itemHolder.position;
  94.             //heldItem.collider.enabled = false;
  95.             networkView.RPC("ItemPickedUp", RPCMode.AllBuffered, playerName, heldItem.name);
  96.             canIthrow = false;
  97.             StartCoroutine(ThrowWaitTime());
  98.            
  99.         }
  100.        
  101.         if (Screen.lockCursor == true && Input.GetButtonDown("Place Block") && areMyHandsFull == true && canIthrow == true)
  102.         {
  103.             heldItem.AddComponent<Rigidbody>();
  104.             Debug.Log("Adding a rigidbody back to this object");
  105.             heldItem.rigidbody.AddForce(myCameraHeadTransform.forward * throwForce, ForceMode.Impulse);
  106.             areMyHandsFull = false;
  107.         }
  108.        
  109.         if (areMyHandsFull == true)
  110.         {
  111.             heldItem.transform.position = itemHolder.transform.position;
  112.             heldItem.transform.rotation = itemHolder.transform.rotation;
  113.         }
  114.     }
  115.    
  116.     [RPC]
  117.     void ItemPickedUp (string pName, string itemName)
  118.     {
  119.         Debug.Log("the held item's name is: " + itemName);
  120.         GameObject hItem = GameObject.Find(itemName);
  121.         Destroy(hItem.GetComponent<Rigidbody>());
  122.        
  123.         //hItem.collider.enabled = false;
  124.     }
  125.    
  126.     IEnumerator ThrowWaitTime()
  127.     {
  128.         yield return new WaitForSeconds(0.5f);
  129.         canIthrow = true;
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement