// Update is called once per frame void Update() { if (fpsOn) { return; } if (Time.timeScale == 1) { deltaT = Time.deltaTime; } else if (Time.timeScale > 1) { deltaT = Time.deltaTime / Time.timeScale; } else { deltaT = 0.015f; } #if UNITY_IPHONE || UNITY_ANDROID || UNITY_WP8 || UNITY_BLACKBERRY if (!UI.IsCursorOnUI(0) && !BuildManager.InDragNDrop()) { if (enableTouchPan) { Quaternion camDir = Quaternion.Euler(0, transform.eulerAngles.y, 0); if (Input.touchCount == 1) { Touch touch = Input.touches[0]; if (touch.phase == TouchPhase.Moved) { Vector3 deltaPos = touch.position; if (lastTouchPos != new Vector3(9999, 9999, 9999)) { deltaPos = deltaPos - lastTouchPos; moveMagnitude = new Vector3(deltaPos.x, 0, deltaPos.y).magnitude *0.1f; moveDir = new Vector3(deltaPos.x, 0, deltaPos.y).normalized *-1; } lastTouchPos = touch.position; if (moveMagnitude > 10) { UIMainControl.ClearSelectedTower(); } } } else { lastTouchPos = new Vector3(9999, 9999, 9999); } Vector3 dir = thisT.InverseTransformDirection(camDir * moveDir) * moveMagnitude; thisT.Translate(dir * panSpeed * deltaT); moveMagnitude = moveMagnitude * (1 - deltaT * 10); } if (enableTouchZoom) { if (Input.touchCount == 2) { Touch touch1 = Input.touches[0]; Touch touch2 = Input.touches[1]; //~ Vector3 zoomScreenPos=(touch1.position+touch2.position)/2; if (touch1.phase == TouchPhase.Moved && touch1.phase == TouchPhase.Moved) { Vector3 dirDelta = (touch1.position - touch1.deltaPosition) - (touch2.position - touch2.deltaPosition); Vector3 dir = touch1.position - touch2.position; float dot = Vector3.Dot(dirDelta.normalized, dir.normalized); if (Mathf.Abs(dot) > 0.7f) { touchZoomSpeed = dir.magnitude - dirDelta.magnitude; } } } currentZoom += Time.deltaTime * zoomSpeed * touchZoomSpeed; touchZoomSpeed = touchZoomSpeed * (1 - Time.deltaTime * 15); } if (enableTouchRotate) { if (Input.touchCount == 2) { Touch touch1 = Input.touches[0]; Touch touch2 = Input.touches[1]; Vector2 delta1 = touch1.deltaPosition.normalized; Vector2 delta2 = touch2.deltaPosition.normalized; Vector2 delta = (delta1 + delta2) / 2; float rotX = thisT.rotation.eulerAngles.x - delta.y * rotationSpeed; float rotY = thisT.rotation.eulerAngles.y + delta.x * rotationSpeed; rotX = Mathf.Clamp(rotX, minRotateAngle, maxRotateAngle); thisT.rotation = Quaternion.Euler(rotX, rotY, 0); } } } #endif #if UNITY_EDITOR || !(UNITY_IPHONE && UNITY_ANDROID && UNITY_WP8 && UNITY_BLACKBERRY) //mouse and keyboard if (enableMouseRotate) { if (Input.GetMouseButtonDown(1)) { initialMousePosX = Input.mousePosition.x; initialMousePosY = Input.mousePosition.y; initialRotX = thisT.eulerAngles.y; initialRotY = thisT.eulerAngles.x; } if (Input.GetMouseButton(1)) { float deltaX = Input.mousePosition.x - initialMousePosX; float deltaRotX = (.1f * (initialRotX / Screen.width)); float rotX = deltaX * rotationSpeed + deltaRotX; float deltaY = initialMousePosY - Input.mousePosition.y; float deltaRotY = -(.1f * (initialRotY / Screen.height)); float rotY = deltaY * rotationSpeed + deltaRotY; float y = rotY + initialRotY; //limit the rotation if (y > maxRotateAngle) { initialRotY -= (rotY + initialRotY) - maxRotateAngle; y = maxRotateAngle; } else if (y < minRotateAngle) { initialRotY += minRotateAngle - (rotY + initialRotY); y = minRotateAngle; } thisT.rotation = Quaternion.Euler(y, rotX + initialRotX, 0); } } Quaternion direction = Quaternion.Euler(0, thisT.eulerAngles.y, 0); if (enableKeyPanning) { if (Input.GetButton("Horizontal")) { Vector3 dir = transform.InverseTransformDirection(direction * Vector3.right); thisT.Translate(dir * panSpeed * deltaT * Input.GetAxisRaw("Horizontal")); } if (Input.GetButton("Vertical")) { Vector3 dir = transform.InverseTransformDirection(direction * Vector3.forward); thisT.Translate(dir * panSpeed * deltaT * Input.GetAxisRaw("Vertical")); } } if (enableMousePanning) { Vector3 mousePos = Input.mousePosition; Vector3 dirHor = transform.InverseTransformDirection(direction * Vector3.right); if (mousePos.x <= 0) { thisT.Translate(dirHor * panSpeed * deltaT * -3); } else if (mousePos.x <= mousePanningZoneWidth) { thisT.Translate(dirHor * panSpeed * deltaT * -1); } else if (mousePos.x >= Screen.width) { thisT.Translate(dirHor * panSpeed * deltaT * 3); } else if (mousePos.x > Screen.width - mousePanningZoneWidth) { thisT.Translate(dirHor * panSpeed * deltaT * 1); } Vector3 dirVer = transform.InverseTransformDirection(direction * Vector3.forward); if (mousePos.y <= 0) { thisT.Translate(dirVer * panSpeed * deltaT * -3); } else if (mousePos.y <= mousePanningZoneWidth) { thisT.Translate(dirVer * panSpeed * deltaT * -1); } else if (mousePos.y >= Screen.height) { thisT.Translate(dirVer * panSpeed * deltaT * 3); } else if (mousePos.y > Screen.height - mousePanningZoneWidth) { thisT.Translate(dirVer * panSpeed * deltaT * 1); } } if (enableMouseZoom) { float zoomInput = Input.GetAxis("Mouse ScrollWheel"); if (zoomInput != 0) { currentZoom += zoomSpeed * zoomInput; currentZoom = Mathf.Clamp(currentZoom, -maxZoomDistance, -minZoomDistance); } } #endif if (avoidClipping) { Vector3 aPos = thisT.TransformPoint(new Vector3(0, 0, currentZoom)); Vector3 dirC = aPos - thisT.position; float dist = Vector3.Distance(aPos, thisT.position); RaycastHit hit; obstacle = Physics.Raycast(thisT.position, dirC, out hit, dist); if (!obstacle) { currentZoom = Mathf.Clamp(currentZoom, -maxZoomDistance, -minZoomDistance); float camZ = Mathf.Lerp(camT.localPosition.z, currentZoom, Time.deltaTime * 4); camT.localPosition = new Vector3(camT.localPosition.x, camT.localPosition.y, camZ); } else { dist = Vector3.Distance(hit.point, thisT.position) * 0.85f; float camZ = Mathf.Lerp(camT.localPosition.z, -dist, Time.deltaTime * 50); camT.localPosition = new Vector3(camT.localPosition.x, camT.localPosition.y, camZ); } } else { currentZoom = Mathf.Clamp(currentZoom, -maxZoomDistance, -minZoomDistance); float camZ = Mathf.Lerp(camT.localPosition.z, currentZoom, Time.deltaTime * 4); camT.localPosition = new Vector3(camT.localPosition.x, camT.localPosition.y, camZ); } float x = Mathf.Clamp(thisT.position.x, minPosX, maxPosX); float z = Mathf.Clamp(thisT.position.z, minPosZ, maxPosZ); thisT.position = new Vector3(x, thisT.position.y, z); }