SHARE
TWEET

Untitled

a guest Mar 17th, 2017 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class CubePickup : MonoBehaviour {
  6.  
  7.     private GameObject m_Player;
  8.     private GameObject m_onHand;
  9.     private GameObject m_Wall;
  10.     private List<GameObject> m_puzzleSlot;
  11.  
  12.     private bool m_pickedUp = false;
  13.  
  14.     // Use this for initialization
  15.     void Start () {
  16.         //Initializing GameObjects
  17.         m_Player = GameObject.FindGameObjectWithTag("Player");
  18.         m_onHand = GameObject.FindGameObjectWithTag("OnHandPosition");
  19.         m_Wall = GameObject.Find("Wall");
  20.  
  21.         m_puzzleSlot = new List<GameObject>(4);
  22.         m_puzzleSlot.Add(GameObject.Find("Slot001"));
  23.         m_puzzleSlot.Add(GameObject.Find("Slot002"));
  24.         m_puzzleSlot.Add(GameObject.Find("Slot003"));
  25.         m_puzzleSlot.Add(GameObject.Find("Slot004"));
  26.     }
  27.    
  28.     // Update is called once per frame
  29.     void Update () {
  30.         //Local Variables
  31.         float distance_block;
  32.         float distance_wall;
  33.  
  34.         //Initializing distances
  35.         distance_block = Vector3.Distance(m_Player.transform.position, transform.position);
  36.         distance_wall = Vector3.Distance(m_Player.transform.position, m_Wall.transform.position);
  37.  
  38.         //Pickup item Handler
  39.         if (Input.GetButtonDown("Fire3") && distance_block < 3)
  40.         {
  41.             m_pickedUp = !m_pickedUp;
  42.         }
  43.  
  44.         if (m_pickedUp)
  45.         {
  46.             GetComponent<Rigidbody>().useGravity = false;
  47.             transform.position = m_onHand.transform.position;
  48.  
  49.             if (Input.GetButtonDown("PlaceBlock"))
  50.             {
  51.                 float shortestDistance = float.MaxValue;
  52.                 GameObject closestEmpty = null;
  53.  
  54.                 for(int i = 0; i < m_puzzleSlot.Count; i++)
  55.                 {
  56.                     float dist = Vector3.Distance(transform.position, m_puzzleSlot[i].transform.position);
  57.  
  58.                     if( dist < shortestDistance)
  59.                     {
  60.                         //check if it's already occupied
  61.                         shortestDistance = dist;
  62.                         closestEmpty = m_puzzleSlot[i];                    
  63.                         //end check if occupied
  64.                     }
  65.                 }
  66.                 m_pickedUp = false;
  67.                 transform.position = closestEmpty.transform.position;
  68.             }
  69.         }
  70.         else
  71.         {
  72.             GetComponent<Rigidbody>().useGravity = true;
  73.         }
  74.     }
  75. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top