CameraMove.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5. public class CameraMove : MonoBehaviour {
  6. Vector2 mousePos;
  7. Camera maincamera;
  8. public float speed;
  9. // Use this for initialization
  10. // Vector3 max
  11. void Start () {
  12. maincamera = Camera.main;
  13. }
  14. // Update is called once per frame
  15. void Update () {
  16. mousePos = Input.mousePosition;
  17. Vector2 max = new Vector2(Camera.main.pixelWidth, Camera.main.pixelHeight);
  18. Vector3 CameraPos = maincamera.transform.position;
  19. if (mousePos.x >= max.x-5) { CameraPos.x += (mousePos.x-(max.x-5)) * Time.deltaTime *speed; }
  20. if (Input.GetKey(KeyCode.D)) { CameraPos.x += 2; }
  21. if(mousePos.x<= 0) { CameraPos.x +=- (5-mousePos.x) * Time.deltaTime * speed; }
  22. if(Input.GetKey(KeyCode.A)) { CameraPos.x -= 2; }
  23. if (mousePos.y >= max.y-5) { CameraPos.z += (mousePos.y-(max.y-5)) * Time.deltaTime * speed; }
  24. if (Input.GetKey(KeyCode.W)) { CameraPos.z += 2; }
  25. if (mousePos.y <= 0) { CameraPos.z +=- (5-mousePos.y) * Time.deltaTime * speed; }
  26. if (Input.GetKey(KeyCode.S)) { CameraPos.z -= 2; }
  27. Clamp(ref CameraPos);
  28. if (CameraPos.y>=20 && CameraPos.y <= 80)
  29. {
  30. CameraPos.y -= Input.GetAxis("Mouse ScrollWheel") * 10;
  31. }
  32. if (CameraPos.y <= 20) CameraPos.y = 20;
  33. if (CameraPos.y >= 80) CameraPos.y = 80;
  34. maincamera.transform.position = CameraPos;
  35. }
  36. void Clamp(ref Vector3 pos)
  37. {
  38. if (pos.x > 300) pos.x = 300;
  39. if (pos.x < 188) pos.x = 188;
  40. if (pos.z > 180) pos.z = 180;
  41. if (pos.z < 45) pos.z = 45;
  42. }
  43. }