Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using VRTK;
- [AddComponentMenu("VRTK/Scripts/Pointers/VRTK_Pointer")]
- public class BlockPlacer : MonoBehaviour
- {
- public bool unlimitedBlocks = false;
- public int blocksLeft = 0;
- public bool lockCursor = true;
- static BlockPlacer obj;
- public GameObject blockPrefab;
- public float range = 7f;
- public VRTK.VRTK_ControllerEvents controllerEvents;
- bool lastTriggerClicked = false;
- void Start()
- {
- BlockPlacer.obj = this;
- var pointer = GetComponentInParent<VRTK_Pointer>();
- }
- void Update()
- {
- bool currentTriggerClicked = controllerEvents.triggerClicked;
- if (blocksLeft > 0 || unlimitedBlocks)
- {
- VRTK_Pointer.Toggle(true);
- if (currentTriggerClicked && currentTriggerClicked != lastTriggerClicked)
- {
- if (blocksLeft > 0 || unlimitedBlocks)
- {
- Ray ray = new Ray(transform.position, transform.TransformDirection(Vector3.forward));
- RaycastHit hit;
- if (Physics.Raycast(ray, out hit, range))
- {
- Vector3 backup = ray.GetPoint(hit.distance - 0.2f);
- Vector3 placeAt = new Vector3(
- Mathf.RoundToInt(backup.x), Mathf.RoundToInt(backup.y), Mathf.RoundToInt(backup.z));
- GameObject block = (GameObject)GameObject.Instantiate(blockPrefab, placeAt, Quaternion.Euler(Vector3.zero));
- if (!unlimitedBlocks)
- blocksLeft--;
- }
- }
- }
- }
- lastTriggerClicked = currentTriggerClicked;
- }
- void OnGUI()
- {
- GUI.Box(new Rect(Screen.width / 2 - 5, Screen.height / 2 - 5, 5, 5), "");
- }
- public static void addBlocks(int i = 1)
- {
- BlockPlacer.obj.blocksLeft += i;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement