Advertisement
Guest User

BlockPlacer

a guest
Nov 14th, 2015
1,555
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.54 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class BlockPlacer : MonoBehaviour {
  5.  
  6.     //Handles # of blocks the user has
  7.     public bool unlimitedBlocks = false;
  8.     public int blocksLeft = 0;
  9.  
  10.     //If the player wants to lock the cursor somewhere else instead, they can do that.
  11.     public bool lockCursor = true;
  12.  
  13.     //static reference to the script. This is so blocks can be added via a static method.
  14.     static BlockPlacer obj;
  15.  
  16.     //The block prefab to instantiate
  17.     public GameObject blockPrefab;
  18.  
  19.     //Maximum range that the player can place a block from
  20.     public float range = 7f;
  21.  
  22.     void Start () {
  23.         //This assigns the static reference
  24.         BlockPlacer.obj = this;
  25.     }
  26.    
  27.     void Update () {
  28.         //Locks the cursor. Running this in update is only done because of weird unity editor shenanigans with cursor locking
  29.        
  30.         if (lockCursor)
  31.             /// ERRORS? Are you getting errors on the following line of code?
  32.             /// If so, you're probably using Unity 4 or earlier.
  33.             /// To fix this, add // to the start of the following line
  34.             Cursor.lockState = CursorLockMode.Locked;
  35.             /// And remove // from the start of the next line of code
  36.             //Cursor.lockCursor = true;
  37.  
  38.         //Make sure the player has enough blocks
  39.         if (blocksLeft > 0 || unlimitedBlocks)
  40.         {
  41.             //Place blocks using the LMB
  42.             if (Input.GetMouseButtonDown(0))
  43.             {
  44.                 //Make a ray and raycasthit for a raycast
  45.                 Ray ray = new Ray(transform.position, transform.TransformDirection(Vector3.forward));
  46.                 RaycastHit hit;
  47.  
  48.                 //Perform the raycast
  49.                 if (Physics.Raycast(ray, out hit, range))
  50.                 {
  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.                     //If the block collides with the player, remove it. We don't want the player to get stuck in their own blocks.
  67.                     if (block.GetComponent<Collider>().bounds.Intersects(this.transform.parent.GetComponent<Collider>().bounds))
  68.                     {
  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.     void OnGUI()
  81.     {
  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 definately 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