Advertisement
mvaganov

BlockPlacer.cs

Feb 12th, 2017
502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.07 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine;
  3. using System.Collections;
  4.  
  5. public class BlockPlacer : MonoBehaviour {
  6.  
  7.     //Handles # of blocks the user has
  8.     public bool unlimitedBlocks = false;
  9.     public int blocksLeft = 0;
  10.  
  11.     //If the player wants to lock the cursor somewhere else instead, they can do that.
  12.     public bool lockCursor = true;
  13.  
  14.     //static reference to the script. This is so blocks can be added via a static method.
  15.     static BlockPlacer obj;
  16.  
  17.     //The block prefab to instantiate
  18.     public GameObject blockPrefab;
  19.  
  20.     //Maximum range that the player can place a block from
  21.     public float range = 7f;
  22.  
  23.     int layerMask = 1 << 8;
  24.     void Start () {
  25.         //This assigns the static reference
  26.         BlockPlacer.obj = this;
  27.         layerMask = ~layerMask;
  28.     }
  29.     void Update () {
  30.         //Locks the cursor. Running this in update is only done because of weird unity editor shenanigans with cursor locking
  31.         if (lockCursor) {
  32.             // ERRORS? Are you getting errors on the following line of code?
  33.             // If so, you're probably using Unity 4 or earlier.
  34.             // To fix this, add
  35.             // to the start of the following line
  36.             Cursor.lockState = CursorLockMode.Locked;
  37.         }
  38.         // And remove
  39.         // from the start of the next line of code
  40.         //Cursor.lockCursor = true;
  41.         //Make sure the player has enough blocks
  42.         if (blocksLeft > 0 || unlimitedBlocks) {
  43.             //Place blocks using the LMB
  44.             if (Input.GetMouseButtonDown(0)) {
  45.                 //Make a ray and raycasthit for a raycast
  46.                 Ray ray = new Ray(transform.position, transform.TransformDirection(Vector3.forward));
  47.                 RaycastHit hit;
  48.  
  49.                 //Perform the raycast
  50.                 if (Physics.Raycast(ray, out hit, range, layerMask)) {
  51.                     //The raycast is backed up so that placing works and won't place blocks inside of the ground.
  52.                     //After testing, 0.2 units back had the best result
  53.                     Vector3 backup = ray.GetPoint(hit.distance - 0.2f);
  54.  
  55.                     //Round the placement so they place like blocks should
  56.                     Vector3 placeAt = new Vector3(
  57.                         Mathf.RoundToInt(backup.x), Mathf.RoundToInt(backup.y), Mathf.RoundToInt(backup.z));
  58.  
  59.                     //Instantiate the block and save it so that we can do other stuff with it later
  60.                     GameObject block = (GameObject)GameObject.Instantiate(blockPrefab, placeAt, Quaternion.Euler(Vector3.zero));
  61.  
  62.                     //Remove a block from the player's "inventory"
  63.                     if (!unlimitedBlocks) {
  64.                         blocksLeft--;
  65.                     }
  66.  
  67.                     //If the block collides with the player, remove it. We don't want the player to get stuck in their own blocks.
  68.                     if (block.GetComponent<Collider>().bounds.Intersects(this.transform.parent.GetComponent<Collider>().bounds)) {
  69.                         //If they are, destroy the block
  70.                         GameObject.Destroy(block);
  71.                         //Make sure that the player gets their misplaced block back.
  72.                         if (!unlimitedBlocks) {
  73.                             blocksLeft++;
  74.                         }
  75.                     }
  76.                 }
  77.             }
  78.         }
  79.     }
  80.  
  81.     void OnGUI() {
  82.         //This is the crosshair
  83.         GUI.Box(new Rect(Screen.width / 2 - 5, Screen.height / 2 - 5, 5, 5), "");
  84.     }
  85.  
  86.     //Static adding of blocks. This isn't needed but definitely helps a lot
  87.     public static void addBlocks(int i = 1) {
  88.         BlockPlacer.obj.blocksLeft += i;
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement