Advertisement
Guest User

Untitled

a guest
Jan 19th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using VRTK;
  4.  
  5. [AddComponentMenu("VRTK/Scripts/Pointers/VRTK_Pointer")]
  6. public class BlockPlacer : MonoBehaviour
  7. {
  8.  
  9.  
  10. public bool unlimitedBlocks = false;
  11. public int blocksLeft = 0;
  12.  
  13.  
  14. public bool lockCursor = true;
  15.  
  16.  
  17. static BlockPlacer obj;
  18.  
  19.  
  20. public GameObject blockPrefab;
  21.  
  22.  
  23. public float range = 7f;
  24. public VRTK.VRTK_ControllerEvents controllerEvents;
  25.  
  26. bool lastTriggerClicked = false;
  27.  
  28. void Start()
  29. {
  30.  
  31. BlockPlacer.obj = this;
  32. var pointer = GetComponentInParent<VRTK_Pointer>();
  33. }
  34.  
  35. void Update()
  36. {
  37. bool currentTriggerClicked = controllerEvents.triggerClicked;
  38.  
  39. if (blocksLeft > 0 || unlimitedBlocks)
  40. {
  41. VRTK_Pointer.Toggle(true);
  42. if (currentTriggerClicked && currentTriggerClicked != lastTriggerClicked)
  43. {
  44.  
  45. if (blocksLeft > 0 || unlimitedBlocks)
  46. {
  47.  
  48. Ray ray = new Ray(transform.position, transform.TransformDirection(Vector3.forward));
  49. RaycastHit hit;
  50.  
  51.  
  52. if (Physics.Raycast(ray, out hit, range))
  53. {
  54.  
  55. Vector3 backup = ray.GetPoint(hit.distance - 0.2f);
  56.  
  57.  
  58. Vector3 placeAt = new Vector3(
  59. Mathf.RoundToInt(backup.x), Mathf.RoundToInt(backup.y), Mathf.RoundToInt(backup.z));
  60.  
  61.  
  62. GameObject block = (GameObject)GameObject.Instantiate(blockPrefab, placeAt, Quaternion.Euler(Vector3.zero));
  63.  
  64.  
  65. if (!unlimitedBlocks)
  66. blocksLeft--;
  67.  
  68.  
  69. }
  70. }
  71. }
  72. }
  73.  
  74. lastTriggerClicked = currentTriggerClicked;
  75. }
  76.  
  77. void OnGUI()
  78. {
  79.  
  80. GUI.Box(new Rect(Screen.width / 2 - 5, Screen.height / 2 - 5, 5, 5), "");
  81. }
  82.  
  83.  
  84. public static void addBlocks(int i = 1)
  85. {
  86. BlockPlacer.obj.blocksLeft += i;
  87. }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement