A team game with an emphasis on movement (with no shooting), inspired by Overwatch and Zineth
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.

121 lines
3.5 KiB

  1. # From khairul169: https://github.com/khairul169/3rdperson-godot/blob/master/assets/scripts/tpscam.gd
  2. extends Spatial
  3. var cam_pitch = 0.0;
  4. var cam_yaw = 0.0;
  5. var cam_cpitch = 0.0;
  6. var cam_cyaw = 0.0;
  7. var cam_currentradius = 2.0;
  8. var cam_radius = 2.0;
  9. var cam_pos = Vector3();
  10. var cam_ray_result = {};
  11. var cam_smooth_movement = true;
  12. var cam_fov = 60.0;
  13. var cam_view_sensitivity = 0.3;
  14. var cam_smooth_lerp = 10;
  15. var cam_pitch_minmax = Vector2(90, -90);
  16. var is_enabled = false;
  17. var collision_exception = [];
  18. export(NodePath) var cam;
  19. export(NodePath) var pivot;
  20. func _ready():
  21. cam = get_node(cam);
  22. pivot = get_node(pivot);
  23. cam_fov = cam.get_fov();
  24. func set_enabled(enabled):
  25. if enabled:
  26. Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED);
  27. set_process(true);
  28. cam.make_current()
  29. is_enabled = true;
  30. else:
  31. Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE);
  32. set_process(false);
  33. is_enabled = false;
  34. func clear_exception():
  35. collision_exception.clear();
  36. func add_collision_exception(node):
  37. collision_exception.push_back(node);
  38. func _input(ie):
  39. if !is_enabled:
  40. return;
  41. if ie is InputEventMouseMotion:
  42. cam_pitch = max(min(cam_pitch+(ie.relative.y*cam_view_sensitivity),cam_pitch_minmax.x),cam_pitch_minmax.y);
  43. if cam_smooth_movement:
  44. cam_yaw = cam_yaw-(ie.relative.x*cam_view_sensitivity);
  45. else:
  46. cam_yaw = fmod(cam_yaw-(ie.relative.x*cam_view_sensitivity),360);
  47. cam_currentradius = cam_radius;
  48. cam_update();
  49. if ie is InputEventMouseButton:
  50. if ie.pressed:
  51. if ie.button_index == BUTTON_WHEEL_UP:
  52. cam_radius = max(min(cam_radius-0.2,4.0),1.0);
  53. elif ie.button_index == BUTTON_WHEEL_DOWN:
  54. cam_radius = max(min(cam_radius+0.2,4.0),1.0);
  55. # Toggle mouse capture:
  56. if Input.is_action_just_pressed("toggle_mouse_capture"):
  57. set_enabled(!is_enabled)
  58. func _process(delta):
  59. if !is_enabled:
  60. return;
  61. if !cam.is_current():
  62. cam.make_current();
  63. if cam.get_projection() == Camera.PROJECTION_PERSPECTIVE:
  64. cam.set_perspective(lerp(cam.get_fov(), cam_fov, cam_smooth_lerp*delta), cam.get_znear(), cam.get_zfar());
  65. if cam_smooth_movement:
  66. cam_cpitch = lerp(cam_cpitch, cam_pitch, 10*delta);
  67. cam_cyaw = lerp(cam_cyaw, cam_yaw, 10*delta);
  68. cam_currentradius = lerp(cam_currentradius, cam_radius, 5*delta);
  69. cam_update();
  70. func cam_update():
  71. cam_pos = pivot.get_global_transform().origin;
  72. if cam_smooth_movement:
  73. cam_pos.x += cam_currentradius * sin(deg2rad(cam_cyaw)) * cos(deg2rad(cam_cpitch));
  74. cam_pos.y += cam_currentradius * sin(deg2rad(cam_cpitch));
  75. cam_pos.z += cam_currentradius * cos(deg2rad(cam_cyaw)) * cos(deg2rad(cam_cpitch));
  76. else:
  77. cam_pos.x += cam_currentradius * sin(deg2rad(cam_yaw)) * cos(deg2rad(cam_pitch));
  78. cam_pos.y += cam_currentradius * sin(deg2rad(cam_pitch));
  79. cam_pos.z += cam_currentradius * cos(deg2rad(cam_yaw)) * cos(deg2rad(cam_pitch));
  80. var pos = Vector3();
  81. if cam_ray_result.size() != 0:
  82. print("re-pos, ray found")
  83. var a = (cam_ray_result.position-pivot.get_global_transform().origin).normalized();
  84. var b = pivot.get_global_transform().origin.distance_to(cam_ray_result.position);
  85. #pos = cam_ray_result.position;
  86. pos = pivot.get_global_transform().origin+a*max(b-0.1, 0);
  87. else:
  88. pos = cam_pos;
  89. cam.look_at_from_position(pos, pivot.get_global_transform().origin, Vector3(0,1,0));
  90. func _physics_process(delta):
  91. if !is_enabled:
  92. return;
  93. var ds = get_world().get_direct_space_state();
  94. if ds != null:
  95. cam_ray_result = ds.intersect_ray(pivot.get_global_transform().origin, cam_pos, collision_exception);
  96. #print(cam_ray_result)