MotionlessState.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace UnityCommon
  5. {
  6. public class MotionlessState : CameraState
  7. {
  8. private Vector3 position;
  9. private Quaternion rotation;
  10. public MotionlessState(Camera camera, Vector3 p, Quaternion r) : base(camera)
  11. {
  12. position = p;
  13. rotation = r;
  14. }
  15. private float clipNearOld = 0.1f;
  16. public override void Enter()
  17. {
  18. base.Enter();
  19. m_camera.transform.SetParent(null);//保持父节点为空,解决相机漂移问题
  20. clipNearOld = m_camera.nearClipPlane;
  21. m_camera.nearClipPlane = 0.1f;
  22. SetCameraPosition(position, rotation, () =>
  23. {
  24. });
  25. }
  26. public override void Exit()
  27. {
  28. base.Exit();
  29. m_camera.nearClipPlane = clipNearOld;
  30. }
  31. private const float speedZoom = 0.5f;
  32. public override void OnZoom(float zoom)
  33. {
  34. base.OnZoom(zoom);
  35. m_camera.transform.Translate(0, 0, zoom * speedZoom);
  36. }
  37. }
  38. }