// Touch to select target void SelectTarget() { // Keep updating till camera move to right place if (requirements.camera.transform.position.y > MinY && MoveToPOI) { changeInCamera = true; } RaycastHit TargetHit; int touchcount = Moba_Camera_Inputs.GetTouchCount(); if (touchcount != 1) { return; } else { Moba_Camera_Inputs.TouchEx touch0 = Moba_Camera_Inputs.GetTouch(0); Vector3 TouchPoint = new Vector3(touch0.position.x, touch0.position.y, 0); // Record touch begin time once Stationary touchs, but need to reject other type of touches if (touch0.phase == TouchPhase.Began && StartTime == 0.0f) { StartTime = Time.time; } else if (touch0.phase == TouchPhase.Stationary && StartTime != 0.0f) { // elapse time big enough if ((Time.time - StartTime) > elapseTimeThreadhold) { CastrayToPOI = true; } } else if (touch0.phase == TouchPhase.Ended || (StartTime != 0 && touch0.phase != TouchPhase.Stationary)) { StartTime = 0.0f; } // Do a ray cast if (CastrayToPOI && Physics.Raycast(requirements.camera.ScreenPointToRay(TouchPoint), out TargetHit, Mathf.Infinity, LayerPOI)) { CastrayToPOI = false; MoveToPOI = true; changeInCamera = true; // Tell the camera update at the first time from here, the rest update lies on the bottom seeting. SelectedTargetPos = TargetHit.point; // requirements.pivot.position = TargetHit.point; Handheld.Vibrate(); } } }
void CalculateCameraMovement() { //////////////////////////////////////////////////////////////////////////////////////////////////// // Camera Movement : When mouse is near the screens edge int touchcount = Moba_Camera_Inputs.GetTouchCount(); if (touchcount <= 0) { return; } Moba_Camera_Inputs.TouchEx touch; touch = Moba_Camera_Inputs.GetTouch(0); // Lock / Unlock camera movement /* if ((touch.phase == TouchPhase.Stationary) && * (settings.lockTargetTransform != null)) * { * //flip bool value * settings.cameraLocked = !settings.cameraLocked; * } * * // if camera is locked or if character focus, set move pivot to target * if(settings.lockTargetTransform != null && (settings.cameraLocked || * ((Input.GetKey(inputs.keycodes.characterFocus)): * (Input.GetButton(inputs.axis.button_char_focus))))) * { * Vector3 target = settings.lockTargetTransform.position; * if((requirements.pivot.position - target).magnitude > 0.2f) { * if(settings.movement.useDefualtHeight * && !settings.movement.useLockTargetHeight) * { * target.y = settings.movement.defualtHeight; * } * else if (!settings.movement.useLockTargetHeight) * { * target.y = requirements.pivot.position.y; * } * * // Lerp between the target and current position * requirements.pivot.position = Vector3.Lerp(requirements.pivot.position, target, settings.movement.lockTransitionRate); * } * } * else * {*/ Vector3 movementVector = new Vector3(0, 0, 0); if (touchcount == 1 && touch.phase == TouchPhase.Moved) { // Get movement of the finger since last frame Vector2 touchDeltaPosition = touch.deltaPosition; // Clamp to (-1, 1) Vector3 temp = Vector3.zero; temp.x = touchDeltaPosition.x / touchDeltaPosition.magnitude; temp.y = touchDeltaPosition.y / touchDeltaPosition.magnitude; Vector3 CamVecForward = requirements.camera.transform.TransformDirection(Vector3.up); Vector3 CamVecRight = requirements.camera.transform.TransformDirection(Vector3.right); // movement is invert with touch direction movementVector = (-temp.x) * CamVecRight + (-temp.y) * CamVecForward; requirements.camera.transform.position += zoomlevel * settings.movement.cameraMovementRate * movementVector * touch.deltaTime; } // } }
void CalculateCameraRotation() { //////////////////////////////////////////////////////////////////////////////////////////////////// // Camera rotate float changeInRotationX = 0.0f; float changeInRotationY = 0.0f; float threshold = 10.0f; //two fingers touch gestures int touchcount = Moba_Camera_Inputs.GetTouchCount(); if (touchcount < 2) { return; } // two finger touches Moba_Camera_Inputs.TouchEx touch0; Moba_Camera_Inputs.TouchEx touch1; touch0 = Moba_Camera_Inputs.GetTouch(0); touch1 = Moba_Camera_Inputs.GetTouch(1); //current position of touches Vector2 currentPos0 = touch0.position; Vector2 currentPos1 = touch1.position; if (touch0.phase == TouchPhase.Moved && touch1.phase == TouchPhase.Moved) { Vector2 vec0 = prePos1 - prePos0; Vector2 vec1 = currentPos1 - currentPos0; float diff = Mathf.Abs(vec0.magnitude - vec1.magnitude); if (diff < threshold) { bRotateAlongCenter = true; bool clockwise; Vector3 cross = Vector3.Cross(vec0, vec1); // determine the roatation guester: counter clockwise or clockwise if (cross.z > 0) { clockwise = true; } else { clockwise = false; } if (!settings.rotation.lockRotationX) { float deltaVertical = currentPos0.y - prePos0.y; if (deltaVertical != 0.0) { if (settings.rotation.constRotationRate) { if (clockwise) { changeInRotationX = -3.0f; } else { changeInRotationX = 3.0f; } } else { changeInRotationX = deltaVertical; } changeInCamera = true; } } if (!settings.rotation.lockRotationY) { float deltaHorizontal = currentPos1.x - prePos1.x; if (deltaHorizontal != 0.0f) { if (settings.rotation.constRotationRate) { if (clockwise) { changeInRotationY = 3.0f; } else { changeInRotationY = -3.0f; } } else { changeInRotationY = deltaHorizontal; } changeInCamera = true; } } } } else { bRotateAlongCenter = false; //reset } // apply change in Y rotation _currentCameraRotation.y += changeInRotationY * settings.rotation.cameraRotationRate.y * Time.deltaTime; _currentCameraRotation.x += changeInRotationX * settings.rotation.cameraRotationRate.x * Time.deltaTime; // restore the position for next touch prePos0 = currentPos0; prePos1 = currentPos1; }
void CalculateCameraZoom() { //////////////////////////////////////////////////////////////////////////////////////////////////// // Camera Zoom In/Out float zoomChange = 0.0f; int inverted = 1; // two fingers touch gesture int touchcount = Moba_Camera_Inputs.GetTouchCount(); if (touchcount < 2) { return; } // two finger touches Moba_Camera_Inputs.TouchEx touch0; Moba_Camera_Inputs.TouchEx touch1; touch0 = Moba_Camera_Inputs.GetTouch(0); touch1 = Moba_Camera_Inputs.GetTouch(1); if (touch0.phase == TouchPhase.Moved && touch1.phase == TouchPhase.Moved) { // current position of touches Vector2 currentPos0 = touch0.position; Vector2 currentPos1 = touch1.position; // caculate the midpoint for two figers to caculate the Raycast if (midpoint.x == 0) { midpoint.x = (currentPos0.x + currentPos1.x) / 2; midpoint.y = (currentPos0.y + currentPos1.y) / 2; bCastZoomRay = true; } //Caculate the gesture float preDistant = (prePos0 - prePos1).magnitude; float curDistant = (currentPos0 - currentPos1).magnitude; // Set the a camera value has changed changeInCamera = true; if (settings.zoom.constZoomRate) { if (preDistant < curDistant) { //Zoom In zoomChange = 3.0f; } else { //Zoom out zoomChange = -3.0f; } } else { zoomChange = curDistant - preDistant; } //change the zoom amount based on if zoom is inverted if (!settings.zoom.invertZoom) { inverted = -1; } _currentZoomAmount += zoomChange * settings.zoom.zoomRate * inverted * Time.deltaTime; // set the new zoom level zoomlevel = (_currentZoomAmount + settings.zoom.defaultZoom) / settings.zoom.defaultZoom; // Cast Zoom Ray to get the hit point if (bCastZoomRay) { RaycastHit hit; // Zoom Raycast only happens on floor layer if (Physics.Raycast(requirements.camera.ScreenPointToRay(midpoint), out hit, Mathf.Infinity, LayerFloor)) { // Set hitposition hitPosition = hit.point; // Reset bool castray bCastZoomRay = false; // Print info on the screen if (guiText == null) { gameObject.AddComponent <GUIText>(); } guiText.text = "Raycast got hit"; } else // missed the hit, do nothing, keep the original pivot { // midpoint.Set (0, 0, 0); } } // leveve the restore to the rotation phase to aviod zero rotation. //prePos0 = currentPos0; //prePos1 = currentPos1; } else { // Reset the midpoint to zero midpoint.Set(0, 0, 0); } }