public void Update() { if (OrbitCamera?.GetComponent <Camera>() == null) { return; } if (FirstUpdate) { var cameraComponent = OrbitCamera.GetComponent <Camera>(); OriginalPosition = OrbitCamera.target.position; OriginalRotation = cameraComponent.transform.rotation; DefaultDistance = OrbitCamera.distance; if (FOV > 0) { cameraComponent.fieldOfView = FOV; } DefaultFOV = cameraComponent.fieldOfView; Console.WriteLine("Setting FOV: " + cameraComponent.fieldOfView); FirstUpdate = false; } HandleHotkeys(); if (Input.GetKey(Modifier)) { HandleRotation(); } else { HandleMovement(); } }
private void HandleRotation() { var cameraComponent = OrbitCamera.GetComponent <Camera>(); if (Input.GetKey(MoveLeft)) { cameraComponent.transform.Rotate(0, TrueSpinRate, 0); } else if (Input.GetKey(MoveRight)) { cameraComponent.transform.Rotate(0, -TrueSpinRate, 0); } if (Input.GetKey(MoveForward)) { cameraComponent.transform.Rotate(TrueSpinRate, 0, 0); } else if (Input.GetKey(MoveBackward)) { cameraComponent.transform.Rotate(-TrueSpinRate, 0, 0); } if (Input.GetKey(MoveUp)) { cameraComponent.transform.Rotate(0, 0, -TrueSpinRate); } else if (Input.GetKey(MoveDown)) { cameraComponent.transform.Rotate(0, 0, TrueSpinRate); } }
public void CubeSwipePerformed(Vector3 hitStart, Vector3 hitEnd) { //Alternatively this could have just been passed from the input manager but this way we can decouple the logic further Physics.Raycast(orbitingCamera.GetComponent <Camera>().ScreenPointToRay(hitStart), out RaycastHit firstHitRaycast); Vector3 firstHit = firstHitRaycast.transform.position; Vector3 firstHitNormal = firstHitRaycast.normal; var projectionPlane = new Plane(firstHitNormal, firstHit); var ray = orbitingCamera.GetComponent <Camera>().ScreenPointToRay(Input.mousePosition); projectionPlane.Raycast(ray, out float enter); var collisionPoint = ray.GetPoint(enter); Vector3Int cubePos = this.magicCube.WorldCoordinatesToCube(firstHit); Vector3Int otherCubePos = this.magicCube.WorldCoordinatesToCube(collisionPoint); Vector3 cubeDelta = otherCubePos - cubePos; //The following section is ugly and could likely have been better fixed with creative usage of the cross //product but unfortunately I was running out of time and some of the edge cases were really weird //Check what the closest world direction to our normal is, use the cube coordinate system offsets to guess where the //player likely wants to rotate to, by figuring out the largest coordinate in that system. //Rotations are flipped if we're on the opposite end of the world vector //In theory the switch could be removed by taking the cross product of the swipe direction // in cube coordinate space and the cube surface normal. //Top if (Vector3.Dot(firstHitNormal, Vector3.up) is float dot1 && Mathf.Abs(dot1) > .9f) { //Debug.Log($"Top"); switch (GetLargestDimensionAsAxis(cubeDelta)) { case CubeRotationAxis.XAxis: PerformRotation(CubeRotationAxis.ZAxis, cubePos.z, Mathf.Sign(dot1) * cubeDelta.x < 0); break; case CubeRotationAxis.ZAxis: PerformRotation(CubeRotationAxis.XAxis, cubePos.x, Mathf.Sign(dot1) * cubeDelta.z > 0); break; } }
private void HandleHotkeys() { var cameraComponent = OrbitCamera.GetComponent <Camera>(); if (Input.GetKeyDown(ToggleFine)) { FineTuneMode ^= true; } if (Input.GetKey(ZoomIn)) { if (Input.GetKey(Modifier)) { OrbitCamera.SetDistance(OrbitCamera.distance - TrueMoveRate); } else { cameraComponent.fieldOfView -= TrueFOVChange; } } else if (Input.GetKey(ZoomOut)) { if (Input.GetKey(Modifier)) { OrbitCamera.SetDistance(OrbitCamera.distance + TrueMoveRate); } else { cameraComponent.fieldOfView += TrueFOVChange; } } if (Input.GetKeyDown(EyeToCam)) { if (GameMain.Instance.CharacterMgr.GetMaidCount() > 0) { GameMain.Instance.CharacterMgr.GetMaid(0).EyeToCamera(EyeToCamMode, 0.8f); EyeToCamMode = EyeToCamMode.NextEnum(1); } } if (Input.GetKey(FOVReset)) { cameraComponent.fieldOfView = DefaultFOV; } if (Input.GetKeyDown(Screenshot)) { MainCamera.ScreenShot(Input.GetKey(Modifier)); } if (Input.GetKey(Reset)) { if (Input.GetKey(Modifier)) { cameraComponent.transform.rotation = OriginalRotation; OrbitCamera.SetDistance(DefaultDistance); } else { OrbitCamera.SetTargetPos(OriginalPosition); } } }
private void Update() { if (inputLock > 0) { return; } //if (Input.GetKeyDown(KeyCode.W)) //{ // gameManager.GameWon(); //} //if (Input.GetKeyUp(KeyCode.R)) //{ // gameManager.RandomRotation(); //} //if (Input.GetKeyDown(KeyCode.U)) //{ // gameManager.UndoLastRotation(); //} //#if !UNITY_ANDROID //If no touches were detected there's a good chance we want to check what the mouse is doing //I could make some platform checking but these days pretty much anything has touch and/or mouse support if (Input.touchCount == 0) { //Mouse zoom is a separate thing orbitingCamera.AddZoomMouseScrollInput(-Input.GetAxis("Mouse ScrollWheel")); if (Input.GetMouseButton(0) && !this.startedSwipeOnCube) { Vector2 mouseDelta = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y")); orbitingCamera.AddMouseInput(mouseDelta); } if (allowRotations) { if (startedSwipeOnCube == true && Input.GetMouseButtonUp(0)) { gameManager.CubeSwipePerformed(this.mouseSwipeStart, Input.mousePosition); this.startedSwipeOnCube = false; } if (Input.GetMouseButtonDown(0)) { if (Physics.Raycast(orbitingCamera.GetComponent <Camera>().ScreenPointToRay(Input.mousePosition), out RaycastHit hit)) { startedSwipeOnCube = true; this.mouseSwipeStart = Input.mousePosition; gameManager.CubeSwipeStarted(hit.transform.position, hit.normal); } } } } //#endif //if (UnityEngine.EventSystems.EventSystem.current.currentSelectedGameObject != null) //{ // return; //} // Handle touch input if (Input.touchCount == 1) { Touch touch = Input.GetTouch(0); if (touch.phase == TouchPhase.Began && touch.phase == TouchPhase.Began) { if (allowRotations) { if (Physics.Raycast(orbitingCamera.GetComponent <Camera>().ScreenPointToRay(Input.mousePosition), out RaycastHit hit)) { startedSwipeOnCube = true; this.mouseSwipeStart = Input.mousePosition; gameManager.CubeSwipeStarted(hit.transform.position, hit.normal); } } } else if (!this.startedSwipeOnCube && touch.phase == TouchPhase.Moved) { orbitingCamera.AddTouchInput(touch.deltaPosition); } else if (this.startedSwipeOnCube && touch.phase == TouchPhase.Ended) { gameManager.CubeSwipePerformed(this.mouseSwipeStart, Input.mousePosition); this.startedSwipeOnCube = false; } } //Pinch to zoom else if (Input.touchCount == 2) { Touch touchZero = Input.GetTouch(0); Touch touchOne = Input.GetTouch(1); if (touchZero.phase != TouchPhase.Moved && touchOne.phase != TouchPhase.Moved) { return; } // Find the position in the previous frame of each touch. Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition; Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition; // Find the magnitude of the vector (the distance) between the touches in each frame. float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude; float touchDeltaMag = (touchZero.position - touchOne.position).magnitude; // Find the difference in the distances between each frame. float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag; orbitingCamera.AddZoomInputPinch(deltaMagnitudeDiff); } }