bitetti

Godot RC 1.1 mouse control template

May 6th, 2015
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.20 KB | None | 0 0
  1. var yaw = 0
  2. var pitch = 0
  3. var in_motion = false
  4. var mouse_motion_scale = 2.5
  5. var mouse_motion = Vector2()
  6. var left_mouse_dow = false
  7. var right_mouse_dow = false
  8. var middle_mouse_dow = false
  9.  
  10. func _input(event):
  11.     if event.type == InputEvent.MOUSE_MOTION:
  12.         #mouse_motion = event.speed / get_viewport().get_rect().size * mouse_motion_ratio
  13.         mouse_motion = event.relative_pos / get_viewport().get_rect().size * mouse_motion_scale
  14.            
  15.     elif event.type == InputEvent.MOUSE_BUTTON:
  16.         if event.button_index == 1:
  17.             left_mouse_dow = event.pressed
  18.         elif event.button_index == 2:
  19.             right_mouse_dow = event.pressed
  20.             in_motion = event.pressed
  21.         elif event.button_index == 3:
  22.             middle_mouse_dow = event.pressed
  23.  
  24.  
  25.  
  26. func _process(delta):
  27.     var transf = get_transform()
  28.    
  29.     if right_mouse_dow:
  30.         if in_motion:
  31.             yaw += mouse_motion.x * PI
  32.             if yaw<-PI:
  33.                 yaw = PI+PI+yaw
  34.             else:
  35.                 if yaw>PI:
  36.                     yaw -= PI + PI
  37.             pitch += mouse_motion.y * PI
  38.             if pitch<-PI:
  39.                 pitch = PI+PI+pitch
  40.             else:
  41.                 if pitch>PI:
  42.                     pitch -= PI + PI
  43.            
  44.             var tr = Transform().rotated(Vector3(0,1,0),yaw).rotated(Vector3(1,0,0),pitch)
  45.             #tr.origin = get_transform().origin
  46.             transf.basis = tr.basis
Advertisement
Add Comment
Please, Sign In to add comment