protected virtual void Translate(Vector2 screenDelta) { // If camera is null, try and get the main camera, return true if a camera was found if (LeanTouch.GetCamera(ref Camera) == true) { // Screen position of the transform var screenPosition = Camera.WorldToScreenPoint(transform.position); // Add the deltaPosition screenPosition += (Vector3)screenDelta; // Convert back to world space transform.position = Camera.ScreenToWorldPoint(screenPosition); } }
protected override void OnSelect(LeanFinger finger) { base.OnSelect(finger); // Make sure the camera exists var camera = LeanTouch.GetCamera(Camera, gameObject); if (camera != null) { // Calculate finger offset var screenPosition = (Vector2)camera.WorldToScreenPoint(transform.position); screenOffset = screenPosition - finger.ScreenPosition; } }
public Vector3 GetSnapshotWorldPosition(float targetAge, float distance, Camera camera = null) { // Make sure the camera exists camera = LeanTouch.GetCamera(camera); if (camera != null) { var screenPosition = GetSnapshotScreenPosition(targetAge); var point = new Vector3(screenPosition.x, screenPosition.y, distance); return(camera.ScreenToWorldPoint(point)); } return(default(Vector3)); }
private void UpdateTranslation() { var finalTransform = Target != null ? Target : transform; // Get the fingers we want to use var fingers = Use.GetFingers(); // Calculate the screenDelta value based on these fingers var screenDelta = LeanGesture.GetScreenDelta(fingers); // Make sure the camera exists var camera = LeanTouch.GetCamera(ScreenDepth.Camera, gameObject); if (fingers.Count == 0) { deltaDifference = Vector2.zero; } if (camera != null) { var worldPosition = finalTransform.position; var oldScreenPoint = camera.WorldToScreenPoint(worldPosition); if (TrackScreenPosition == true) { if (ScreenDepth.TryConvert(ref worldPosition, oldScreenPoint + (Vector3)(screenDelta + deltaDifference), gameObject) == true) { finalTransform.position = worldPosition; } var newScreenPoint = camera.WorldToScreenPoint(worldPosition); var oldNewDelta = (Vector2)(newScreenPoint - oldScreenPoint); deltaDifference += screenDelta - oldNewDelta; } else { if (ScreenDepth.TryConvert(ref worldPosition, oldScreenPoint + (Vector3)screenDelta, gameObject) == true) { finalTransform.position = worldPosition; } } } else { Debug.LogError("Failed to find camera. Either tag your cameras MainCamera, or set one in this component.", this); } }
/// <summary>This will return the ray of the finger's start position relative to the specified camera (none/null = Main Camera).</summary> public Ray GetStartRay(Camera camera = null) { // Make sure the camera exists camera = LeanTouch.GetCamera(camera); if (camera != null) { return(camera.ScreenPointToRay(StartScreenPosition)); } else { Debug.LogError("Failed to find camera. Either tag your cameras MainCamera, or set one in this component."); } return(default(Ray)); }
/// <summary>This will return the change in world position of this finger based on the last and current distance from the camera.</summary> public Vector3 GetWorldDelta(float lastDistance, float distance, Camera camera = null) { // Make sure the camera exists camera = LeanTouch.GetCamera(camera); if (camera != null) { return(GetWorldPosition(distance, camera) - GetLastWorldPosition(lastDistance, camera)); } else { Debug.LogError("Failed to find camera. Either tag your cameras MainCamera, or set one in this component."); } return(default(Vector3)); }
protected virtual void Rotate(float twistDegrees) { // Make sure the camera exists var camera = LeanTouch.GetCamera(Camera, gameObject); if (camera != null) { var axis = transform.InverseTransformDirection(camera.transform.forward); transform.rotation *= Quaternion.AngleAxis(twistDegrees, axis); } else { Debug.LogError("Failed to find camera. Either tag your cameras MainCamera, or set one in this component.", this); } }
protected virtual void Update() { // Does the path exist? if (Path != null) { // Is this GameObject selected? if (Selectable.IsSelected == true) { // Does it have a selected finger? var finger = Selectable.SelectingFinger; if (finger != null) { // Make sure the camera exists var camera = LeanTouch.GetCamera(Camera, gameObject); if (camera != null) { // Offset finger screen position var screenPosition = finger.ScreenPosition + screenOffset; // Find offset ray for this finger var ray = camera.ScreenPointToRay(screenPosition); // Move GameObject to closest point, if one was found var closestPoint = default(Vector3); if (Path.TryGetClosest(ray, ref closestPoint, Smoothing) == true) { transform.position = closestPoint; } } } } if (AutoSnap == true) { // Move GameObject to closest point, if one was found var closestPoint = default(Vector3); if (Path.TryGetClosest(transform.position, ref closestPoint, Smoothing) == true) { transform.position = closestPoint; } } } }
// Override the WritePositions method from LeanDragLine protected virtual void WritePositions(Link link) { // Make sure the camera exists var camera = LeanTouch.GetCamera(Camera, gameObject); if (camera == null) { return; } // Get start and current world position of finger var screenPoint = camera.WorldToScreenPoint(link.Target.transform.position); var start = link.Target.transform.position; var end = link.Finger.GetWorldPosition(screenPoint.z); var distance = Vector3.Distance(start, end); // Limit the length? if (LengthLimit > 0.0f && distance > LengthLimit) { var direction = Vector3.Normalize(end - start); distance = LengthLimit; end = start + direction * distance; } if (Invert == true) { end = start + (start - end); } // Write positions var thickness = distance * ThicknessScale; #if UNITY_OLD_LINE_RENDERER link.Line.SetVertexCount(2); link.Line.SetWidth(thickness, thickness); #else link.Line.positionCount = 2; link.Line.startWidth = thickness; link.Line.endWidth = thickness; #endif link.Line.SetPosition(0, start); link.Line.SetPosition(1, end); }
protected virtual void LateUpdate() { // Make sure the camera exists var camera = LeanTouch.GetCamera(Camera, gameObject); if (camera != null) { // Get the fingers we want to use var fingers = LeanTouch.GetFingers(IgnoreGuiFingers, RequiredFingerCount); // Get the world delta of all the fingers var worldDelta = LeanGesture.GetWorldDelta(fingers, Distance, camera); // Pan the camera based on the world delta transform.position -= worldDelta * Sensitivity; } }
protected virtual void Translate(Vector2 screenDelta) { // verifica se a camera existe var camera = LeanTouch.GetCamera(Camera, gameObject); if (camera != null) { // posicao da tela de tranformacao var screenPoint = camera.WorldToScreenPoint(transform.position); // adiciona o deltaPosicao screenPoint += (Vector3)screenDelta; // converte de volta ao espaco na tela transform.position = camera.ScreenToWorldPoint(screenPoint); } }
protected void SetZoom(float current) { // Make sure the camera exists var camera = LeanTouch.GetCamera(Camera, gameObject); if (camera != null) { if (camera.orthographic == true) { camera.orthographicSize = current; } else { camera.fieldOfView = current; } } }
/// <summary>This allows you to click the screen when using a perspective camera, and have the Pitch and Yaw values update to that point.</summary> public void RotateToFinger(LeanFinger finger) { var camera = LeanTouch.GetCamera(Camera, gameObject); if (camera != null) { var xyz = finger.GetRay(camera).direction; var longitude = Mathf.Atan2(xyz.x, xyz.z); var latitude = Mathf.Asin(xyz.y / xyz.magnitude); var newPitch = latitude * -Mathf.Rad2Deg; var newYaw = longitude * Mathf.Rad2Deg; var delta = Mathf.DeltaAngle(Yaw, newYaw); Pitch = newPitch; Yaw += delta; } }
protected virtual void Translate(Vector2 screenDelta) { // Make sure the camera exists var camera = LeanTouch.GetCamera(Camera, gameObject); if (camera != null) { // Screen position of the transform Vector3 screenPosition = camera.WorldToScreenPoint(transform.position); // Add the deltaPosition screenPosition += (Vector3)screenDelta; // Convert back to world space transform.position = camera.ScreenToWorldPoint(screenPosition); } }
protected virtual void Update() { // Get the fingers we want to use var fingers = LeanSelectable.GetFingers(IgnoreStartedOverGui, IgnoreIsOverGui, RequiredFingerCount, RequiredSelectable); // Calculate the screenDelta value based on these fingers var screenDelta = LeanGesture.GetScreenDelta(fingers); // Make sure the camera exists var camera = LeanTouch.GetCamera(Camera, gameObject); if (fingers.Count == 0) { deltaDifference = Vector2.zero; } if (camera != null) { if (TrackScreenPosition == true) { var oldScreenPoint = camera.WorldToScreenPoint(transform.position); var worldPosition = default(Vector3); if (ScreenDepth.TryConvert(ref worldPosition, oldScreenPoint + (Vector3)(screenDelta + deltaDifference), camera, gameObject) == true) { transform.position = worldPosition; } var newScreenPoint = camera.WorldToScreenPoint(transform.position); var oldNewDelta = (Vector2)(newScreenPoint - oldScreenPoint); deltaDifference += screenDelta - oldNewDelta; } else { var oldScreenPoint = camera.WorldToScreenPoint(transform.position); var worldPosition = default(Vector3); if (ScreenDepth.TryConvert(ref worldPosition, oldScreenPoint + (Vector3)screenDelta, camera, gameObject) == true) { transform.position = worldPosition; } } } }
protected virtual void Translate(Vector2 screenDelta) { // Make sure the camera exists var camera = LeanTouch.GetCamera(Camera, gameObject); if (camera != null) { // Screen position of the transform var screenPoint = camera.WorldToScreenPoint(transform.position); // Add the deltaPosition screenPoint += (Vector3)screenDelta; Vector3 originalPos = new Vector3(transform.position.x, transform.position.y, transform.position.z); // Convert back to world space transform.position = camera.ScreenToWorldPoint(screenPoint); transform.position = new Vector3(originalPos.x, transform.position.y, originalPos.z); } }
/// <summary>This will return the world position of this finger based on the distance from the camera.</summary> public Vector3 GetWorldPosition(float distance, Camera camera = null) { // Make sure the camera exists camera = LeanTouch.GetCamera(camera); if (camera != null) { var point = new Vector3(ScreenPosition.x, ScreenPosition.y, distance); return(camera.ScreenToWorldPoint(point)); } else { Debug.LogError("Failed to find camera. Either tag your cameras MainCamera, or set one in this component."); } return(default(Vector3)); }
public void Apply(Vector2 screenDelta) { // Make sure the camera exists var camera = LeanTouch.GetCamera(Camera, gameObject); if (camera != null) { var oldPoint = transform.position; var screenPoint = camera.WorldToScreenPoint(oldPoint); screenPoint.x += screenDelta.x; screenPoint.y += screenDelta.y; var newPoint = camera.ScreenToWorldPoint(screenPoint); ApplyBetween(oldPoint, newPoint); } }
protected override void LateUpdate() { // Make sure the camera exists if (LeanTouch.GetCamera(ref Camera, gameObject) == true) { // Use the LateUpdate code from LeanCameraZoom base.LateUpdate(); // Get t value var factor = LeanTouch.GetDampenFactor(Dampening, Time.deltaTime); // Lerp the current value to the target one currentZoom = Mathf.Lerp(currentZoom, Zoom, factor); // Set the new zoom SetZoom(currentZoom); } }
protected virtual void Translate(float pinchScale, Vector2 screenCenter) { // Make sure the camera exists var camera = LeanTouch.GetCamera(Camera, gameObject); if (camera != null) { // Screen position of the transform var screenPosition = camera.WorldToScreenPoint(transform.position); // Push the screen position away from the reference point based on the scale screenPosition.x = screenCenter.x + (screenPosition.x - screenCenter.x) * pinchScale; screenPosition.y = screenCenter.y + (screenPosition.y - screenCenter.y) * pinchScale; // Convert back to world space transform.position = camera.ScreenToWorldPoint(screenPosition); } }
protected virtual void Translate(float pinchScale, Vector2 screenCenter) { // certifica de que a câmera existe var camera = LeanTouch.GetCamera(Camera, gameObject); if (camera != null) { // pega posição da tela da transformação var screenPosition = camera.WorldToScreenPoint(transform.position); // afasta a posição da tela do ponto de referência com base na escala screenPosition.x = screenCenter.x + (screenPosition.x - screenCenter.x) * pinchScale; screenPosition.y = screenCenter.y + (screenPosition.y - screenCenter.y) * pinchScale; // Converter de volta ao espaço mundial transform.position = camera.ScreenToWorldPoint(screenPosition); } }
private void TranslateUI(Vector2 screenDelta) { var camera = LeanTouch.GetCamera(Camera, gameObject); // Screen position of the transform var screenPoint = RectTransformUtility.WorldToScreenPoint(camera, transform.position); // Add the deltaPosition screenPoint += screenDelta; // Convert back to world space var worldPoint = default(Vector3); if (RectTransformUtility.ScreenPointToWorldPointInRectangle(transform.parent as RectTransform, screenPoint, camera, out worldPoint) == true) { transform.position = worldPoint; } }
private void Scale(float pinchScale, Vector2 screenCenter, RaycastHit hit) { // Make sure the scale is valid if (pinchScale > 0.0f) { var scale = hit.transform.localScale; if (Relative == true) { // Make sure the camera exists var camera = LeanTouch.GetCamera(Camera, gameObject); if (camera != null) { // Screen position of the transform var screenPosition = camera.WorldToScreenPoint(hit.transform.position); // Push the screen position away from the reference point based on the scale screenPosition.x = screenCenter.x + (screenPosition.x - screenCenter.x) * pinchScale; screenPosition.y = screenCenter.y + (screenPosition.y - screenCenter.y) * pinchScale; // Convert back to world space hit.transform.position = camera.ScreenToWorldPoint(screenPosition); // Grow the local scale by scale scale *= pinchScale; } } else { // Grow the local scale by scale scale *= pinchScale; } if (ScaleClamp == true) { scale.x = Mathf.Clamp(scale.x, ScaleMin.x, ScaleMax.x); scale.y = Mathf.Clamp(scale.y, ScaleMin.y, ScaleMax.y); scale.z = Mathf.Clamp(scale.z, ScaleMin.z, ScaleMax.z); } hit.transform.localScale = scale; } }
private Vector3 GetStartPoint(LeanFinger finger) { // Use target position? if (Target != null) { return(Target.position); } // Make sure the camera exists var camera = LeanTouch.GetCamera(Camera, gameObject); if (camera != null) { // Get start and current world position of finger return(finger.GetStartWorldPosition(Distance, camera)); } return(default(Vector3)); }
private void Rotate(Vector3 center, float degrees) { if (Relative == true) { // If camera is null, try and get the main camera, return true if a camera was found if (LeanTouch.GetCamera(ref Camera) == true) { // World position of the reference point var worldReferencePoint = Camera.ScreenToWorldPoint(center); // Rotate the transform around the world reference point transform.RotateAround(worldReferencePoint, Camera.transform.forward, degrees); } } else { transform.rotation *= Quaternion.AngleAxis(degrees, RotateAxis); } }
protected override void Translate(Vector2 screenDelta) { // Make sure the camera exists var camera = LeanTouch.GetCamera(Camera, gameObject); if (camera != null) { // Store old position var oldPosition = transform.position; // Screen position of the transform var screenPosition = camera.WorldToScreenPoint(oldPosition); // Add the deltaPosition screenPosition += (Vector3)screenDelta; // Convert back to world space var newPosition = camera.ScreenToWorldPoint(screenPosition); // Add to delta RemainingDelta += newPosition - oldPosition; } }
private void Scale(float pinchScale, Vector2 screenCenter) { // Make sure the scale is valid if (pinchScale > 0.0f) { var scale = transform.localScale; if (Relative == true) { // If camera is null, try and get the main camera, return true if a camera was found if (LeanTouch.GetCamera(ref Camera) == true) { // Screen position of the transform var screenPosition = Camera.WorldToScreenPoint(transform.position); // Push the screen position away from the reference point based on the scale screenPosition.x = screenCenter.x + (screenPosition.x - screenCenter.x) * pinchScale; screenPosition.y = screenCenter.y + (screenPosition.y - screenCenter.y) * pinchScale; // Convert back to world space transform.position = Camera.ScreenToWorldPoint(screenPosition); // Grow the local scale by scale scale *= pinchScale; } } else { // Grow the local scale by scale scale *= pinchScale; } if (ScaleClamp == true) { scale.x = Mathf.Clamp(scale.x, ScaleMin.x, ScaleMax.x); scale.y = Mathf.Clamp(scale.y, ScaleMin.y, ScaleMax.y); scale.z = Mathf.Clamp(scale.z, ScaleMin.z, ScaleMax.z); } transform.localScale = scale; //transform.position = arController.itemSpawnPos; } }
protected virtual void TranslateUI(float pinchScale, Vector2 pinchScreenCenter) { var camera = LeanTouch.GetCamera(Camera, gameObject); // Screen position of the transform var screenPoint = RectTransformUtility.WorldToScreenPoint(camera, transform.position); // Push the screen position away from the reference point based on the scale screenPoint.x = pinchScreenCenter.x + (screenPoint.x - pinchScreenCenter.x) * pinchScale; screenPoint.y = pinchScreenCenter.y + (screenPoint.y - pinchScreenCenter.y) * pinchScale; // Convert back to world space var worldPoint = default(Vector3); if (RectTransformUtility.ScreenPointToWorldPointInRectangle(transform.parent as RectTransform, screenPoint, camera, out worldPoint) == true) { transform.position = worldPoint; } }
protected virtual void Translate(Vector2 screenDelta, RaycastHit hit) { // Make sure the camera exists var camera = LeanTouch.GetCamera(Camera, gameObject); if (camera != null) { Debug.Log("Calculating new position..."); Debug.Log(hit.transform.gameObject.name); // Screen position of the transform var screenPosition = camera.WorldToScreenPoint(hit.transform.position); // Add the deltaPosition screenPosition += (Vector3)screenDelta; // Convert back to world space hit.transform.position = camera.ScreenToWorldPoint(screenPosition); } }
private Vector3 GetPoint(Vector3 axis, Vector2 screenPoint) { // Make sure the camera exists var camera = LeanTouch.GetCamera(Camera, gameObject); if (camera != null) { var ray = camera.ScreenPointToRay(screenPoint); var plane = new Plane(axis, transform.position); var distance = default(float); if (plane.Raycast(ray, out distance) == true) { return(ray.GetPoint(distance)); } } return(oldPoint); }