// Update is called once per frame void Update() { //Do we have a Pointer Bead to position in the world? if (laserPointerBeadHolder != null) { Vector3 crosshairpoint; Vector3 crosshairnormal; //Point the bead at the closest thing in front of the camera. if (ZEDManager.Instance.IsZEDReady && FindCrosshairPosition(out crosshairpoint, out crosshairnormal)) { //We hit something. Make sure the bead is active. pointerbead.SetActive(true); //Position the bead a the collision point, and make it face you. pointerbead.transform.position = crosshairpoint; if (crosshairnormal.magnitude > 0.0001f) { pointerbead.transform.rotation = Quaternion.LookRotation(crosshairnormal); } } else { //We didn't hit anything. Disable the bead object. pointerbead.SetActive(false); } } //Check to see if any valid fire keys are triggered. //This is more complex than often necessary so as to work out-of-the-box for a variety of configurations. //If using/extending this script for your own project, it's recommended to use Input.GetButtonDown() with a custom Input, use Unity.XR, or interface with a VR SDK. bool buttondown = false; //Check for keys present on all systems if (Input.GetKeyDown(KeyCode.Mouse0) || Input.GetKeyDown(KeyCode.Space)) { buttondown = true; } #if ZED_OCULUS //Update whether the Touch controllers are active. int children = transform.childCount; if (OVRManager.isHmdPresent) { if (OVRInput.GetConnectedControllers().ToString() == "Touch") { for (int i = 0; i < children; ++i) { transform.GetChild(i).gameObject.SetActive(true); } } else { for (int i = 0; i < children; ++i) { transform.GetChild(i).gameObject.SetActive(false); } } } //We're controlling the fire Rate. OVRInput doesn't have a GetDown function for the IndexTrigger. Only an axis output. if (objecttracker != null) { if (OVRInput.GetConnectedControllers().ToString() == "Touch") { if ((int)objecttracker.deviceToTrack == 0) { if (fireCount == 0 && OVRInput.Get(OVRInput.Axis1D.PrimaryIndexTrigger, OVRInput.Controller.RTouch) > 0.75f) { buttondown = true; fireCount++; } else if (OVRInput.Get(OVRInput.Axis1D.PrimaryIndexTrigger, OVRInput.Controller.RTouch) < 0.75f) { fireCount = 0; } } if ((int)objecttracker.deviceToTrack == 1) { if (fireCount == 0 && OVRInput.Get(OVRInput.Axis1D.PrimaryIndexTrigger, OVRInput.Controller.LTouch) > 0.75f) { buttondown = true; fireCount++; } else if (OVRInput.Get(OVRInput.Axis1D.PrimaryIndexTrigger, OVRInput.Controller.LTouch) < 0.75f) { fireCount = 0; } } } OVRInput.Update(); } #endif #if ZED_STEAM_VR //Looks for any input from this controller through SteamVR if (objecttracker != null) { if ((int)objecttracker.deviceToTrack == 0 && objecttracker.index >= 0) { //if (SteamVR_Controller.Input((int)objecttracker.index).GetHairTriggerDown()) if (objecttracker.GetVRButtonDown(Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger)) { buttondown = true; } } if ((int)objecttracker.deviceToTrack == 1 && objecttracker.index >= 0) { //if (SteamVR_Controller.Input((int)objecttracker.index).GetHairTriggerDown()) if (objecttracker.GetVRButtonDown(Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger)) { buttondown = true; } } } #endif if (buttondown) { Fire(); } }
/// <summary> /// Update is called every frame. /// Here we receive the input from the Controller. /// Then we decide what to do in each case. /// </summary> private void Update() { if (tracker == null) { if (Input.GetKeyDown(KeyCode.Space)) { button = state.Down; } else if (Input.GetKey(KeyCode.Space)) { button = state.Press; } else if (Input.GetKeyUp(KeyCode.Space)) { button = state.Up; } else { button = state.Idle; } } else { #if ZED_STEAM_VR //Check if a Controller tracked. if ((int)tracker.index > 0) { //device = SteamVR_Controller.Input((int)tracker.index); } //SteamVR provides OnButton responses for the Trigger input. //When pressing down, holding it, or releasing it. if ((int)tracker.index > 0) { //if (device.GetPressDown(SteamVR_Controller.ButtonMask.Trigger)) if (tracker.GetVRButtonDown(Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger)) { button = state.Down; } //else if (device.GetPress(SteamVR_Controller.ButtonMask.Trigger)) else if (tracker.GetVRButtonHeld(Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger)) { button = state.Press; } //else if (device.GetPressUp(SteamVR_Controller.ButtonMask.Trigger)) else if (tracker.GetVRButtonReleased(Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger)) { button = state.Up; } else { button = state.Idle; } } #elif ZED_OCULUS //Check if a Controller is tracked. if ((int)tracker.deviceToTrack == 0) { //Oculus Touch Triggers aren't of Button type, but Axis. //So we have to create our own state for this Input, based on sensitivity from 0 to 1. if (OVRInput.Get(OVRInput.Axis1D.PrimaryIndexTrigger, OVRInput.Controller.RTouch) > 0.5f) { if (button == state.Idle) { button = state.Down; } else if (button == state.Down) { button = state.Press; } } else { if (button == state.Press || button == state.Down) { button = state.Up; } else if (button == state.Up) { button = state.Idle; } } } if ((int)tracker.deviceToTrack == 1) { if (OVRInput.Get(OVRInput.Axis1D.PrimaryIndexTrigger, OVRInput.Controller.LTouch) > 0.5f) { if (button == state.Idle) { button = state.Down; } else if (button == state.Down) { button = state.Press; } } else { if (button == state.Press || button == state.Down) { button = state.Up; } else if (button == state.Up) { button = state.Idle; } } } #endif } //If the Trigger Button is being used. if (button != state.Idle) { //It just got pressed. if (button == state.Down) { //Enable the bunnySpawner to display the placeholder. bunnySpawner.canDisplayPlaceholder = true; //If we were holding the baseball bat but the user wants to re-place the bunny, hide the baseball bat. if (bunnySpawner.baseballBat != null) { bunnySpawner.baseballBat.SetActive(false); } //Clean up the list of detected planes. if (zedPlane.hitPlaneList.Count > 0) { for (int i = 0; i < zedPlane.hitPlaneList.Count; i++) { Destroy(zedPlane.hitPlaneList[i].gameObject); zedPlane.hitPlaneList.RemoveAt(i); } } //Destroy the current Bunny, if any, on the scene. if (!bunnySpawner.canSpawnMultipleBunnies && bunnySpawner.currentBunny != null) { Destroy(bunnySpawner.currentBunny); bunnySpawner.currentBunny = null; } } //From the first input to the next ones as it keeps being hold down. if (button == state.Press || button == state.Down) { if (zedPlane.hitPlaneList.Count == 0) { //Start detecting planes through the ZED Plane Detection Manager. if (zedPlane.DetectPlaneAtHit(leftCamera.WorldToScreenPoint(placeholder.position))) { //Get the normal of the plane. ZEDPlaneGameObject currentPlane = zedPlane.getHitPlane(zedPlane.hitPlaneList.Count - 1); Vector3 planeNormal = currentPlane.worldNormal; //Check if the plane has a normal close enough to Y (horizontal surface) to be stable for the Bunny to spawn into. if (Vector3.Dot(planeNormal, Vector3.up) > 0.85f) { //Allow spawning the Bunny, and set the placeholder to a positive color. if (canspawnbunny == false) { canspawnbunny = true; bunnySpawner.placeHolderMat[0].mainTexture = goodPlacementTex[0]; bunnySpawner.placeHolderMat[1].mainTexture = goodPlacementTex[1]; pointlight.color = Color.blue; } else //Clear the list of planes. { for (int i = 0; i < zedPlane.hitPlaneList.Count; i++) { if (i == 0) { Destroy(zedPlane.hitPlaneList[i].gameObject); zedPlane.hitPlaneList.RemoveAt(i); } } } } else //Surface wasn't horizontal enough { //Don't allow the Bunny to spawn, and set the placeholder to a negative color. canspawnbunny = false; bunnySpawner.placeHolderMat[0].mainTexture = badPlacementTex[0]; bunnySpawner.placeHolderMat[1].mainTexture = badPlacementTex[1]; pointlight.color = Color.red; //Clear the list of planes. for (int i = 0; i < zedPlane.hitPlaneList.Count; i++) { Destroy(zedPlane.hitPlaneList[i].gameObject); zedPlane.hitPlaneList.RemoveAt(i); } } } } else if (zedPlane.hitPlaneList.Count > 0) { if (!Physics.Raycast(transform.position, placeholder.position - transform.position)) { //Don't allow for the Bunny to spawn, and set the placeholder to a negative color. canspawnbunny = false; bunnySpawner.placeHolderMat[0].mainTexture = badPlacementTex[0]; bunnySpawner.placeHolderMat[1].mainTexture = badPlacementTex[1]; pointlight.color = Color.red; //Clear the list of planes. for (int i = 0; i < zedPlane.hitPlaneList.Count; i++) { Destroy(zedPlane.hitPlaneList[i].gameObject); zedPlane.hitPlaneList.RemoveAt(i); } } } } //Button is released. if (button == state.Up) { //If at that moment the bunny was allowed to spawn, proceed ot make the call. if (canspawnbunny) { bunnySpawner.SpawnBunny(placeholder.position); } else //Clear the list of planes. { for (int i = 0; i < zedPlane.hitPlaneList.Count; i++) { Destroy(zedPlane.hitPlaneList[i].gameObject); zedPlane.hitPlaneList.RemoveAt(i); } } //Reset the booleans. canspawnbunny = false; bunnySpawner.canDisplayPlaceholder = false; } } }