Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- // This script is attached to each player. It allows them to pick up objects.
- public class PlayerPickupScript : MonoBehaviour {
- // Declare Variables
- // The range of the cast ray
- private float range = 1.5f;
- // The name of the ray cast
- RaycastHit hit;
- // Is the object I'm targeting able to be picked up?
- public bool canBePickedUp = false;
- // The player can only pick up an object if they aren't currently holding one
- private bool areMyHandsFull = false;
- // Prevent the player from throwing an object the second it is picked up
- private bool canIthrow = false;
- private float throwForce = 10;
- // References
- private Transform myTransform;
- private Transform myCameraHeadTransform;
- private Transform itemHolder;
- private GameObject heldItem;
- private IdentifyPickupScript identifyPickupScript;
- private PickupMovementUpdate pickupMovementUpdate;
- private string playerName;
- // Use this for initialization
- void Start ()
- {
- myTransform = transform;
- myCameraHeadTransform = myTransform.Find("CameraHead");
- itemHolder = myTransform.Find("ItemHolder");
- identifyPickupScript = myTransform.GetComponent<IdentifyPickupScript>();
- playerName = PlayerPrefs.GetString("playerName");
- /* if (networkView.isMine == true)
- {
- myTransform = transform;
- // The ray is cast based on the direction the camera head is facing
- // Remember that the camera head tilts up and down because of
- // the MouseLook script attached to it
- myCameraHeadTransform = myTransform.Find("CameraHead");
- itemHolder = myTransform.Find("ItemHolder");
- identifyPickupScript = myTransform.GetComponent<IdentifyPickupScript>();
- playerName = PlayerPrefs.GetString("playerName");
- }
- else
- {
- enabled = false;
- } */
- }
- // Update is called once per frame
- void Update ()
- {
- // Fire a ray from the cameraHead heading straight forward. NOTE: THIS HAPPENS In THE IdentifyPickupScript now
- /* if (Physics.Raycast (myCameraHeadTransform.position, myCameraHeadTransform.forward, out hit, range))
- {
- if (hit.transform.tag == "Pickup")
- {
- canBePickedUp = true;
- }
- }
- if (Physics.Raycast (myCameraHeadTransform.position, myCameraHeadTransform.forward, out hit))
- {
- if (hit.transform.tag != "Pickup")
- {
- canBePickedUp = false;
- }
- } */
- // I'm going to continue use the Place Block input for now since this is frankensteined from the PlayerAsksServerToPlaceConstructionBlock script
- if (Screen.lockCursor == true && Input.GetButtonDown("Place Block") && areMyHandsFull == false && canBePickedUp == true)
- {
- heldItem = identifyPickupScript.hit.transform.gameObject;
- Debug.Log("Picking up: " + heldItem);
- areMyHandsFull = true;
- identifyPickupScript.hit.transform.position = itemHolder.position;
- //heldItem.collider.enabled = false;
- networkView.RPC("ItemPickedUp", RPCMode.AllBuffered, playerName, heldItem.name);
- canIthrow = false;
- StartCoroutine(ThrowWaitTime());
- }
- if (Screen.lockCursor == true && Input.GetButtonDown("Place Block") && areMyHandsFull == true && canIthrow == true)
- {
- heldItem.AddComponent<Rigidbody>();
- Debug.Log("Adding a rigidbody back to this object");
- heldItem.rigidbody.AddForce(myCameraHeadTransform.forward * throwForce, ForceMode.Impulse);
- areMyHandsFull = false;
- }
- if (areMyHandsFull == true)
- {
- heldItem.transform.position = itemHolder.transform.position;
- heldItem.transform.rotation = itemHolder.transform.rotation;
- }
- }
- [RPC]
- void ItemPickedUp (string pName, string itemName)
- {
- Debug.Log("the held item's name is: " + itemName);
- GameObject hItem = GameObject.Find(itemName);
- Destroy(hItem.GetComponent<Rigidbody>());
- //hItem.collider.enabled = false;
- }
- IEnumerator ThrowWaitTime()
- {
- yield return new WaitForSeconds(0.5f);
- canIthrow = true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement