EditorCameraMovement.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*===============================================================================
  2. Copyright (C) 2022 Immersal - Part of Hexagon. All Rights Reserved.
  3. This file is part of the Immersal SDK.
  4. The Immersal SDK cannot be copied, distributed, or made available to
  5. third-parties for commercial purposes without written permission of Immersal Ltd.
  6. Contact sdk@immersal.com for licensing requests.
  7. ===============================================================================*/
  8. using UnityEngine;
  9. namespace Immersal.Samples.ContentPlacement
  10. {
  11. public class EditorCameraMovement : MonoBehaviour
  12. {
  13. [SerializeField]
  14. private float m_CameraMoveSpeed = 3f;
  15. [SerializeField]
  16. private float m_MoveSpeedMultiplier = 4f;
  17. [SerializeField]
  18. private Vector2 m_CameraSensitivity = new Vector2(15f, 15f);
  19. [SerializeField]
  20. private bool m_FlyMode = true;
  21. private Vector3 m_PointerDownScreenPos;
  22. private Vector3 m_PointerScreenPos;
  23. private float m_RotationY = 0f;
  24. private void Update()
  25. {
  26. #if UNITY_EDITOR
  27. if (Input.GetKeyDown(KeyCode.LeftShift))
  28. {
  29. m_CameraMoveSpeed *= m_MoveSpeedMultiplier;
  30. }
  31. if (Input.GetKeyUp(KeyCode.LeftShift))
  32. {
  33. m_CameraMoveSpeed /= m_MoveSpeedMultiplier;
  34. }
  35. if (Input.GetKey(KeyCode.W))
  36. {
  37. MoveCamera(Vector3.forward);
  38. }
  39. if (Input.GetKey(KeyCode.A))
  40. {
  41. MoveCamera(Vector3.left);
  42. }
  43. if (Input.GetKey(KeyCode.S))
  44. {
  45. MoveCamera(Vector3.back);
  46. }
  47. if (Input.GetKey(KeyCode.D))
  48. {
  49. MoveCamera(Vector3.right);
  50. }
  51. if (Input.GetMouseButton(1))
  52. {
  53. float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * m_CameraSensitivity.x;
  54. m_RotationY += Input.GetAxis("Mouse Y") * m_CameraSensitivity.y;
  55. m_RotationY = Mathf.Clamp(m_RotationY, -60f, 60f);
  56. Vector3 leuler = new Vector3(-m_RotationY, rotationX, 0f);
  57. transform.localRotation = Quaternion.Slerp(transform.localRotation, Quaternion.Euler(leuler), Time.deltaTime * 32f);
  58. }
  59. #endif
  60. }
  61. private void MoveCamera(Vector3 direction)
  62. {
  63. Vector3 movement = transform.TransformDirection(direction) * m_CameraMoveSpeed * Time.deltaTime;
  64. if (!m_FlyMode)
  65. {
  66. movement.y = 0f;
  67. }
  68. transform.Translate(movement, Space.World);
  69. }
  70. }
  71. }