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.

117 lines
3.3 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(89, -89);
  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. cam.make_current()
  28. else:
  29. Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE);
  30. set_process(enabled);
  31. is_enabled = enabled;
  32. func clear_exception():
  33. collision_exception.clear();
  34. func add_collision_exception(node):
  35. collision_exception.push_back(node);
  36. func _input(ie):
  37. if !is_enabled:
  38. return;
  39. if ie is InputEventMouseMotion:
  40. cam_pitch = max(min(cam_pitch+(ie.relative.y*cam_view_sensitivity),cam_pitch_minmax.x),cam_pitch_minmax.y);
  41. if cam_smooth_movement:
  42. cam_yaw = cam_yaw-(ie.relative.x*cam_view_sensitivity);
  43. else:
  44. cam_yaw = fmod(cam_yaw-(ie.relative.x*cam_view_sensitivity),360);
  45. cam_currentradius = cam_radius;
  46. cam_update();
  47. if ie is InputEventMouseButton:
  48. if ie.pressed:
  49. if ie.button_index == BUTTON_WHEEL_UP:
  50. cam_radius = max(min(cam_radius-0.2,4.0),1.0);
  51. elif ie.button_index == BUTTON_WHEEL_DOWN:
  52. cam_radius = max(min(cam_radius+0.2,4.0),1.0);
  53. # Toggle mouse capture:
  54. if Input.is_action_just_pressed("toggle_mouse_capture"):
  55. set_enabled(!is_enabled)
  56. func _process(delta):
  57. if !is_enabled:
  58. return;
  59. if !cam.is_current():
  60. cam.make_current();
  61. if cam.get_projection() == Camera.PROJECTION_PERSPECTIVE:
  62. cam.set_perspective(lerp(cam.get_fov(), cam_fov, cam_smooth_lerp*delta), cam.get_znear(), cam.get_zfar());
  63. if cam_smooth_movement:
  64. cam_cpitch = lerp(cam_cpitch, cam_pitch, 10*delta);
  65. cam_cyaw = lerp(cam_cyaw, cam_yaw, 10*delta);
  66. cam_currentradius = lerp(cam_currentradius, cam_radius, 5*delta);
  67. cam_update();
  68. func cam_update():
  69. cam_pos = pivot.get_global_transform().origin;
  70. if cam_smooth_movement:
  71. cam_pos.x += cam_currentradius * sin(deg2rad(cam_cyaw)) * cos(deg2rad(cam_cpitch));
  72. cam_pos.y += cam_currentradius * sin(deg2rad(cam_cpitch));
  73. cam_pos.z += cam_currentradius * cos(deg2rad(cam_cyaw)) * cos(deg2rad(cam_cpitch));
  74. else:
  75. cam_pos.x += cam_currentradius * sin(deg2rad(cam_yaw)) * cos(deg2rad(cam_pitch));
  76. cam_pos.y += cam_currentradius * sin(deg2rad(cam_pitch));
  77. cam_pos.z += cam_currentradius * cos(deg2rad(cam_yaw)) * cos(deg2rad(cam_pitch));
  78. var pos = Vector3();
  79. if cam_ray_result.size() != 0:
  80. var a = (cam_ray_result.position-pivot.get_global_transform().origin).normalized();
  81. var b = pivot.get_global_transform().origin.distance_to(cam_ray_result.position);
  82. pos = pivot.get_global_transform().origin+a*max(b-0.1, 0);
  83. else:
  84. pos = cam_pos;
  85. cam.look_at_from_position(pos, pivot.get_global_transform().origin, Vector3(0,1,0));
  86. func _physics_process(delta):
  87. if !is_enabled:
  88. return;
  89. var ds = get_world().get_direct_space_state();
  90. if ds != null:
  91. cam_ray_result = ds.intersect_ray(pivot.get_global_transform().origin, cam_pos, collision_exception);