alexsosnovskiy

Minecraft controller for block creation

Jan 11th, 2017
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.65 KB | None | 0 0
  1. /*
  2. Author: afrokick
  3. Forum: www.devby.ru
  4. Video: https://www.youtube.com/watch?v=Zgwv_YZaXJA
  5. */
  6. using UnityEngine;
  7. using System.Collections;
  8. public class MineCraftController : MonoBehaviour {
  9.  
  10. public GameObject block;
  11. public Material mat_blue;
  12. public Material mat_red;
  13. // Use this for initialization
  14. void Start () {
  15.   Screen.showCursor = false;
  16. }
  17.  
  18. // Update is called once per frame
  19. void Update () {
  20.   Ray ray = Camera.mainCamera.ScreenPointToRay(new Vector3(Screen.width/2,Screen.height/2,0));
  21.   RaycastHit hit;
  22.  
  23.   if(Physics.Raycast(ray,out hit,5f) && hit.collider.tag == "Block")
  24.   {  
  25.    if(Input.GetMouseButtonUp(0))
  26.    {
  27.         Vector3 pos = hit.collider.transform.position;
  28.         pos += hit.normal;
  29.  
  30.         Instantiate(block,pos,Quaternion.identity);
  31.    }
  32.    else if(Input.GetMouseButtonUp(1))
  33.    {
  34.         DestroyObject(hit.collider.gameObject);
  35.    }
  36.   }else if(Physics.Raycast(ray,out hit,5f) && hit.collider.name == "Plane")
  37.   {
  38.  
  39.    if(Input.GetMouseButtonUp(0))
  40.    {
  41.         Vector3 pos = hit.point;
  42.         pos.y = 0;
  43.         pos.x = Mathf.Round(pos.x);
  44.         pos.z = Mathf.Round(pos.z);
  45.  
  46.         Instantiate(block,pos,Quaternion.identity);
  47.    }
  48.   }
  49. }
  50.  
  51. void OnGUI()
  52. {
  53.   if(Input.GetKey(KeyCode.M))
  54.   {
  55.    Screen.showCursor = true;
  56.  
  57.    GUILayout.BeginVertical();
  58.    GUILayout.Label(block.renderer.sharedMaterial.name + "");
  59.  
  60.    if(GUILayout.Button("Blue"))
  61.    {
  62.         block.renderer.sharedMaterial = mat_blue;
  63.    }
  64.    if(GUILayout.Button("Red"))
  65.    {
  66.         block.renderer.sharedMaterial = mat_red;
  67.    }
  68.    GUILayout.EndVertical();
  69.   }
  70.   else
  71.   {
  72.    Screen.showCursor = false;
  73.   }
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment