void DoTriggerClick(object sender, ClickedEventArgs e) { if (teleportOnClick) { // First get the current Transform of the the reference space (i.e. the Play Area, e.g. CameraRig prefab) var t = reference; if (t == null) { return; } // Get the current Y position of the reference space float refY = t.position.y; // Create a plane at the Y position of the Play Area // Then create a Ray from the origin of the controller in the direction that the controller is pointing Plane plane = new Plane(Vector3.up, -refY); Ray ray = new Ray(this.transform.position, transform.forward); // Set defaults bool hasGroundTarget = false; float dist = 0f; if (teleportType == TeleportType.TeleportTypeUseTerrain) // If we picked to use the terrain { RaycastHit hitInfo; TerrainCollider tc = Terrain.activeTerrain.GetComponent <TerrainCollider>(); hasGroundTarget = tc.Raycast(ray, out hitInfo, 1000f); dist = hitInfo.distance; } else if (teleportType == TeleportType.TeleportTypeUseCollider) // If we picked to use the collider { RaycastHit hitInfo; hasGroundTarget = Physics.Raycast(ray, out hitInfo); dist = hitInfo.distance; } else // If we're just staying flat on the current Y axis { // Intersect a ray with the plane that was created earlier // and output the distance along the ray that it intersects hasGroundTarget = plane.Raycast(ray, out dist); } if (hasGroundTarget) { // Get the current Camera (head) position on the ground relative to the world //Vector3 headPosOnGround = new Vector3(SteamVR_Render.Top().head.position.x, refY, SteamVR_Render.Top().head.position.z); // We need to translate the reference space along the same vector // that is between the head's position on the ground and the intersection point on the ground // i.e. intersectionPoint - headPosOnGround = translateVector // currentReferencePosition + translateVector = finalPosition /* * Vector3 createPoint = new Vector3(); * createPoint = t.position + (ray.origin + (ray.direction * dist)) + new Vector3(0,0,0); * Instantiate(createObj, createPoint, Quaternion.identity); //[createObjSelected] */ if (selectingStep == selectingStepEnum.units) { slpoint1 = (ray.origin + (ray.direction * dist)); //t.position + selectingStep = selectingStepEnum.target; } else if (selectingStep == selectingStepEnum.target) { targetPoint = (ray.origin + (ray.direction * dist)); //t.position + selectingStep = selectingStepEnum.none; GameObject[] list = GameObject.FindGameObjectsWithTag("Player's Unit"); ArrayList listSelected = new ArrayList(); for (int i = 0; i < list.Length; i++) { if (list[i].GetComponent <PlayerUnitController>().selected) { listSelected.Add(list[i]); } } float width = Mathf.Sqrt(listSelected.Count); // listSelected.Count; // Mathf.Sqrt(listSelected.Count); if (listSelected.Count == 1) { ((GameObject)listSelected[0]).BroadcastMessage("Target", targetPoint); } else { int row = 0, col = 0; for (int i = 0; i < listSelected.Count; i++) { ((GameObject)listSelected[i]).BroadcastMessage("Target", targetPoint + new Vector3((col) * .3f - width / 2 * .3f, 0, (row) * .3f - width / 2 * .3f)); slpoint1 = new Vector3(0, 0, 0); slpoint2 = new Vector3(0, 0, 0); col++; if (col > width) { col = 0; row++; } } } } //teleport //t.position = t.position + (ray.origin + (ray.direction * dist)) - headPosOnGround; } } }
private void Update() { var trackedController = GetComponent <SteamVR_TrackedController>(); //////////////PAD SWIPING FOR TOOL SELECTION////////////////// SteamVR_TrackedObject trackedObj = GetComponent <SteamVR_TrackedObject>(); var device = SteamVR_Controller.Input((int)trackedObj.index); float px = device.GetAxis(Valve.VR.EVRButtonId.k_EButton_Axis0).x; float py = device.GetAxis(Valve.VR.EVRButtonId.k_EButton_Axis0).y; if (padTouched && Mathf.Abs(py) < .3) { selectionTimer = 30; if (!swipingHorizontally && !swipingVertically) { if (lastPx != 0 && Math.Abs(px - lastPx) > .4) { swipingHorizontally = true; } else if (lastPy != 0 && Math.Abs(py - lastPy) > .2) { swipingVertically = true; } } if (swipingHorizontally && px != 0 && lastPx != 0) { buildingRotation -= (px - lastPx) * 120; //* (padXPrev - e.padX); } if (swipingVertically && py != 0 && lastPy != 0) { float magnitude = transform.parent.gameObject.transform.localScale.magnitude; transform.parent.gameObject.transform.localScale += new Vector3(1, 1, 1) * (py - lastPy) * magnitude; magnitude = transform.parent.gameObject.transform.localScale.magnitude; if (magnitude < .25) { transform.parent.gameObject.transform.localScale = new Vector3(1, 1, 1) * .25f; } if (magnitude > 500) { transform.parent.gameObject.transform.localScale = new Vector3(1, 1, 1) * 500; } //magnitude = transform.parent.gameObject.transform.localScale.magnitude; //Debug.Log(magnitude); } if (swipingHorizontally) { scale = (scale + 1) / 2; } } else { if (selectionTimer > 0) { selectionTimer -= 20 * Time.deltaTime; } else { selectionTimer = 0; } if (selectionTimer < 18) { swipingHorizontally = false; swipingVertically = false; } if (selectionTimer < 10) { scale = Mathf.Lerp(0, 1, selectionTimer / 10); //(scale + 0) / 2; } buildingRotation = (Mathf.Round(buildingRotation / 90) * 90 + buildingRotation) / 2; } buildingSelector.localScale = new Vector3(scale, scale, scale); buildingSelector.eulerAngles = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, transform.eulerAngles.z + buildingRotation); //store previous position of pad lastPx = px; lastPy = py; //////////////////////////////////// // First get the current Transform of the the reference space (i.e. the Play Area, e.g. CameraRig prefab) var t = reference; if (t == null) { return; } // Get the current Y position of the reference space float refY = t.position.y; Plane plane = new Plane(Vector3.up, -refY); Ray ray = new Ray(this.transform.position, transform.forward); bool hasGroundTarget = false; float dist = 0f; RaycastHit hitInfo; TerrainCollider tc; // = Terrain.activeTerrain.GetComponent<TerrainCollider>(); //dist = hitInfo.distance; if (teleportType == TeleportType.TeleportTypeUseTerrain) // If we picked to use the terrain { //RaycastHit hitInfo; tc = Terrain.activeTerrain.GetComponent <TerrainCollider>(); hasGroundTarget = tc.Raycast(ray, out hitInfo, 1000f); dist = hitInfo.distance; } else if (teleportType == TeleportType.TeleportTypeUseCollider) // If we picked to use the collider { //RaycastHit hitInfo; hasGroundTarget = Physics.Raycast(ray, out hitInfo); dist = hitInfo.distance; } else // If we're just staying flat on the current Y axis { // Intersect a ray with the plane that was created earlier // and output the distance along the ray that it intersects hasGroundTarget = plane.Raycast(ray, out dist); } if (trackedController.triggerPressed) { // Create a plane at the Y position of the Play Area // Then create a Ray from the origin of the controller in the direction that the controller is pointing // Set defaults //bool hasGroundTarget = false; //float dist = 0f; if (hasGroundTarget && selectingStep == selectingStepEnum.target) { slpoint2 = (ray.origin + (ray.direction * dist)); //t.position + } } else { if (selectingStep == selectingStepEnum.none) { selectingStep = selectingStepEnum.units; } } if (selectingStep == selectingStepEnum.target) { //x1 to x2 on z1 DrawLine(new Vector3(slpoint1.x, slpoint1.y + .01f, slpoint1.z), new Vector3(slpoint2.x, slpoint1.y + .01f, slpoint1.z), Color.blue); //x1 to x2 on z2 DrawLine(new Vector3(slpoint1.x, slpoint1.y + .01f, slpoint2.z), new Vector3(slpoint2.x, slpoint1.y + .01f, slpoint2.z), Color.blue); //z1 to z2 on x1 DrawLine(new Vector3(slpoint1.x, slpoint1.y + .01f, slpoint1.z), new Vector3(slpoint1.x, slpoint1.y + .01f, slpoint2.z), Color.blue); //z1 to z2 on x2 DrawLine(new Vector3(slpoint2.x, slpoint1.y + .01f, slpoint1.z), new Vector3(slpoint2.x, slpoint1.y + .01f, slpoint2.z), Color.blue); } Transform placePrefab = turretPrefab; bool placing = false; if (buildingRotation > 360) { buildingRotation -= 360; } if (buildingRotation < 0) { buildingRotation += 360; } if (Mathf.Round(buildingRotation) == 0) { placeObj = place_obj_turret; placePrefab = turretPrefab; placing = true; } if (Mathf.Round(buildingRotation) == 270) { placeObj = place_obj_wall; placePrefab = wallPrefab; placing = true; } if (Mathf.Round(buildingRotation) == 180) { placeObj = place_obj_hut; placePrefab = hutPrefab; placing = true; } GameObject[] cam = GameObject.FindGameObjectsWithTag("MainCamera"); placeObj.position = (ray.origin + (ray.direction * dist)); placeObj.eulerAngles = new Vector3(0, -transform.eulerAngles.z - cam[0].transform.eulerAngles.y, 0); if (trackedController.triggerPressed) { if (!clicked && placing) { clicked = true; //Debug.Log("Instantiated"); Transform newobj = Instantiate(placePrefab, placeObj.position, new Quaternion(0, 0, 0, 0)); newobj.eulerAngles = new Vector3(0, transform.eulerAngles.z, 0); } } else { clicked = false; } }
private void Update() { var trackedController = GetComponent <SteamVR_TrackedController>(); if (trackedController.triggerPressed) { // First get the current Transform of the the reference space (i.e. the Play Area, e.g. CameraRig prefab) var t = reference; if (t == null) { return; } // Get the current Y position of the reference space float refY = t.position.y; // Create a plane at the Y position of the Play Area // Then create a Ray from the origin of the controller in the direction that the controller is pointing Plane plane = new Plane(Vector3.up, -refY); Ray ray = new Ray(this.transform.position, transform.forward); // Set defaults bool hasGroundTarget = false; float dist = 0f; if (teleportType == TeleportType.TeleportTypeUseTerrain) // If we picked to use the terrain { RaycastHit hitInfo; TerrainCollider tc = Terrain.activeTerrain.GetComponent <TerrainCollider>(); hasGroundTarget = tc.Raycast(ray, out hitInfo, 1000f); dist = hitInfo.distance; } else if (teleportType == TeleportType.TeleportTypeUseCollider) // If we picked to use the collider { RaycastHit hitInfo; hasGroundTarget = Physics.Raycast(ray, out hitInfo); dist = hitInfo.distance; } else // If we're just staying flat on the current Y axis { // Intersect a ray with the plane that was created earlier // and output the distance along the ray that it intersects hasGroundTarget = plane.Raycast(ray, out dist); } if (hasGroundTarget && selectingStep == selectingStepEnum.target) { slpoint2 = t.position + (ray.origin + (ray.direction * dist)); } } else { if (selectingStep == selectingStepEnum.none) { selectingStep = selectingStepEnum.units; Debug.Log("SelectingStep set to units"); } } if (selectingStep == selectingStepEnum.target) { //x1 to x2 on z1 DrawLine(new Vector3(slpoint1.x, slpoint1.y + .1f, slpoint1.z), new Vector3(slpoint2.x, slpoint1.y + .1f, slpoint1.z), Color.blue); //x1 to x2 on z2 DrawLine(new Vector3(slpoint1.x, slpoint1.y + .1f, slpoint2.z), new Vector3(slpoint2.x, slpoint1.y + .1f, slpoint2.z), Color.blue); //z1 to z2 on x1 DrawLine(new Vector3(slpoint1.x, slpoint1.y + .1f, slpoint1.z), new Vector3(slpoint1.x, slpoint1.y + .1f, slpoint2.z), Color.blue); //z1 to z2 on x2 DrawLine(new Vector3(slpoint2.x, slpoint1.y + .1f, slpoint1.z), new Vector3(slpoint2.x, slpoint1.y + .1f, slpoint2.z), Color.blue); } }