Advertisement
Guest User

Working camera

a guest
Feb 23rd, 2018
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. extends Camera
  2.  
  3. # class member variables go here, for example:
  4. # var a = 2
  5. # var b = "textvar"
  6.  
  7. var windowsize;
  8. var viewport;
  9. var moveCameraOnX=0;
  10. var moveCameraOnY=0;
  11.  
  12. func _ready():
  13. Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED); #hide cursor
  14. viewport=get_viewport();
  15. windowsize=viewport.get_visible_rect().size;
  16. pass
  17.  
  18. func _input(event):
  19. if event is InputEventMouseMotion:
  20. _moveCameraWithMouse(event);
  21. pass
  22.  
  23. #
  24. func _process(delta):
  25. # Called every frame. Delta is time since last frame.
  26. # Update game logic here.
  27. pass
  28.  
  29. func _moveCameraWithMouse(event):
  30.  
  31. var mousePositionChangeSinceLastFrame = event.relative;
  32. var sensitivity = 0.01;
  33. moveCameraOnX = mousePositionChangeSinceLastFrame.x * sensitivity;
  34. moveCameraOnY = mousePositionChangeSinceLastFrame.y * sensitivity;
  35.  
  36. var potentialCameraXAngleOnNextFrame = rad2deg(rotation.x+moveCameraOnY);
  37. if (_isBetween(potentialCameraXAngleOnNextFrame, -90, 90)):
  38. rotation += Vector3(-moveCameraOnY, -moveCameraOnX,0); #rotate camera. moveCameraOnY actually rotates on X axis
  39. #"OnY" stands for "uses mouse Y position". Same for moveCameraOnX
  40. #so thats why moveCameraOnY is on the "X" position in the Vector3
  41.  
  42. #print(rad2deg(rotation.x),' ', rad2deg(rotation.y),' ', rad2deg(rotation.z));
  43.  
  44.  
  45. #triggerMouseMotionEventsEnabled=false;
  46. #viewport.warp_mouse(Vector2(windowsize.x/2, windowsize.y/2));
  47. #triggerMouseMotionEventsEnabled=true;
  48. #TODO: explain the moveCameraOnX/Y swap better (moveCameraOnX actually rotates on Y axis so thats confusing)
  49. pass
  50.  
  51. func _isBetween(x, bound1,bound2):
  52. if (bound1 > bound2):
  53. _swap(bound1, bound2); #bound1 always smaller than bound2
  54.  
  55. #print(x, '>=', bound1, x>=bound1);
  56. #print(x, '<=', bound2, x<=bound2);
  57.  
  58. if(x >= bound1 and x<=bound2):
  59. return true;
  60. else: return false;
  61. pass
  62.  
  63. func _swap(a, b):
  64. var temp = a;
  65. a = b;
  66. b = temp;
  67. pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement