Advertisement
darthbator

Untitled

Oct 15th, 2012
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.26 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class orthoCameraMan : MonoBehaviour {
  5.     public Transform target;
  6.     public Vector3 offset;
  7.     public float minSize;
  8.     public float maxSize;
  9.     public float lowClamp;
  10.     public float highClamp;
  11.     public float dragSpeed;
  12.     public float camFocusSpeed;
  13.     public float camFocusTurnSpeed;
  14.    
  15.     private float cameraMovement;
  16.     private RaycastHit hit;
  17.     private int loopCount;
  18.    
  19.     void Start () {
  20.        
  21.     }
  22.    
  23.     void LateUpdate () {
  24.         if (target != null) {
  25.             transform.LookAt(target);
  26.             transform.position = target.position + offset;
  27.         }
  28.        
  29.         //pinch and pull to zoom the camera
  30.         if (Input.touchCount == 2) {
  31.             target = null; //unset any camera targeting
  32.             Touch firstFinger = Input.GetTouch(0);
  33.             Touch secondFinger = Input.GetTouch(1);
  34.             float touchDistance = Vector2.Distance(firstFinger.position, secondFinger.position);
  35.             if (firstFinger.phase == TouchPhase.Moved || secondFinger.phase == TouchPhase.Moved) {
  36.                 float lastDistance = Vector2.Distance(firstFinger.position - firstFinger.deltaPosition, secondFinger.position - secondFinger.deltaPosition);
  37.                 float moveDistance = Mathf.Clamp(touchDistance - lastDistance, lowClamp, highClamp);
  38.                 Camera.main.orthographicSize = Mathf.Clamp(Camera.main.orthographicSize - moveDistance, minSize, maxSize);
  39.             }
  40.         }
  41.         if (Input.touchCount == 1 &&
  42.                 Input.GetTouch(0).tapCount == 2 &&
  43.                 rayAtTouch(out hit, Input.GetTouch(0).position) &&
  44.                 hit.collider.gameObject.layer == (int)Layers.Index.playerActorLayer)
  45.         {
  46.             target = hit.collider.gameObject.transform;
  47.             StartCoroutine(repositionCam());
  48.         }
  49.     }
  50.    
  51.     public void dragCamera (Vector3 scrollDist) {
  52.         Vector3 normalizeScrollDist = new Vector3(scrollDist.x * Time.deltaTime * dragSpeed,
  53.             0, scrollDist.z * Time.deltaTime * dragSpeed);
  54.         transform.position += normalizeScrollDist;
  55.     }
  56.    
  57.     IEnumerator repositionCam () {
  58.         while (cameraMovement < 1) {
  59.             cameraMovement += camFocusSpeed * Time.deltaTime;
  60.             transform.position = Vector3.Lerp(transform.position, transform.position + offset, cameraMovement);
  61.             yield return null;
  62.         }
  63.     }
  64.    
  65.     bool rayAtTouch (out RaycastHit rayHit, Vector3 touchLoc) {
  66.         Ray ray = Camera.main.ScreenPointToRay(touchLoc);
  67.         return Physics.Raycast(ray, out rayHit);
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement