The Swartz-Manning’s first exhibit will provide a detailed history of Aaron Swartz Day. https://www.aaronswartzday.org/vr
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CameraControls.cs 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /**
  2. * Camera orbit controls.
  3. */
  4. using UnityEngine;
  5. using System.Collections;
  6. namespace ProBuilder2.Examples
  7. {
  8. public class CameraControls : MonoBehaviour
  9. {
  10. const string INPUT_MOUSE_SCROLLWHEEL = "Mouse ScrollWheel";
  11. const string INPUT_MOUSE_X = "Mouse X";
  12. const string INPUT_MOUSE_Y = "Mouse Y";
  13. const float MIN_CAM_DISTANCE = 10f;
  14. const float MAX_CAM_DISTANCE = 40f;
  15. // how fast the camera orbits
  16. [Range(2f, 15f)]
  17. public float orbitSpeed = 6f;
  18. // how fast the camera zooms in and out
  19. [Range(.3f,2f)]
  20. public float zoomSpeed = .8f;
  21. // the current distance from pivot point (locked to Vector3.zero)
  22. float distance = 0f;
  23. // how fast the idle camera movement is
  24. public float idleRotation = 1f;
  25. private Vector2 dir = new Vector2(.8f, .2f);
  26. void Start()
  27. {
  28. distance = Vector3.Distance(transform.position, Vector3.zero);
  29. }
  30. void LateUpdate()
  31. {
  32. Vector3 eulerRotation = transform.localRotation.eulerAngles;
  33. eulerRotation.z = 0f;
  34. // orbits
  35. if( Input.GetMouseButton(0) )
  36. {
  37. float rot_x = Input.GetAxis(INPUT_MOUSE_X);
  38. float rot_y = -Input.GetAxis(INPUT_MOUSE_Y);
  39. eulerRotation.x += rot_y * orbitSpeed;
  40. eulerRotation.y += rot_x * orbitSpeed;
  41. // idle direction is derived from last user input.
  42. dir.x = rot_x;
  43. dir.y = rot_y;
  44. dir.Normalize();
  45. }
  46. else
  47. {
  48. eulerRotation.y += Time.deltaTime * idleRotation * dir.x;
  49. eulerRotation.x += Time.deltaTime * Mathf.PerlinNoise(Time.time, 0f) * idleRotation * dir.y;
  50. }
  51. transform.localRotation = Quaternion.Euler( eulerRotation );
  52. transform.position = transform.localRotation * (Vector3.forward * -distance);
  53. if( Input.GetAxis(INPUT_MOUSE_SCROLLWHEEL) != 0f )
  54. {
  55. float delta = Input.GetAxis(INPUT_MOUSE_SCROLLWHEEL);
  56. distance -= delta * (distance/MAX_CAM_DISTANCE) * (zoomSpeed * 1000) * Time.deltaTime;
  57. distance = Mathf.Clamp(distance, MIN_CAM_DISTANCE, MAX_CAM_DISTANCE);
  58. transform.position = transform.localRotation * (Vector3.forward * -distance);
  59. }
  60. }
  61. }
  62. }