Posted By


joseques on 02/24/14

Tagged


Statistics


Viewed 136 times
Favorited by 0 user(s)

CameraFollower.cs


/ Published in: C#
Save to your folder(s)

For UnityAnswers


Copy this code and paste it in your HTML
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class CameraLimiter : MonoBehaviour {
  5. public float minX = 0;
  6. public float maxX = 90;
  7. public float minY = 0;
  8. public float maxY = 10;
  9. public float minSize = 3;
  10. public float maxSize = 10;
  11. public float zoomSpeed = 10;
  12. public float deftZoom = 5;
  13. private bool reset;
  14. void Update () {
  15. float X = Mathf.Clamp(transform.position.x,minX ,maxX);
  16. float Y = Mathf.Clamp(transform.position.y,minY ,maxY);
  17. transform.position = new Vector3(X, Y, transform.position.z);
  18. checkZoom();
  19. }
  20.  
  21. void checkZoom(){
  22. if(Input.GetButton("zoomIn") || Input.GetAxis("Mouse ScrollWheel") > 0){
  23. float clamped = Mathf.Clamp(camera.orthographicSize-(Time.deltaTime*zoomSpeed), minSize, maxSize);
  24. camera.orthographicSize = clamped;
  25. } else if(Input.GetButton("zoomOut") || Input.GetAxis("Mouse ScrollWheel") < 0){
  26. float clamped = Mathf.Clamp(camera.orthographicSize+(Time.deltaTime*zoomSpeed), minSize, maxSize);
  27. camera.orthographicSize = clamped;
  28. } else if(Input.GetButton("resetZoom") || Input.GetMouseButtonUp(2)){
  29. reset = true;
  30. StartCoroutine("cameraReset");
  31. }
  32. }
  33. IEnumerator cameraReset() {
  34. while(reset){
  35. camera.orthographicSize = Mathf.Lerp(camera.orthographicSize, deftZoom, (Time.deltaTime*zoomSpeed));
  36. if(Mathf.Approximately(camera.orthographicSize, deftZoom)) reset = false;
  37. yield return null;
  38. }
  39. }
  40. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.