Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Bisdante |
Hello everyone, i am trying to make a game where the player can preview the trajectory of their projectiles with a line, but the game crashes when trying to draw it. No error messages, it just stops responding, so i’m assuming i’m calling something too much, but i don’t know what. This is the code:
func get_predicted_path(player):
if Input.is_action_pressed("left"):
rotation_degrees -= 1
if Input.is_action_pressed("right"):
rotation_degrees += 1
var starting_point = player.global_position
var end_point = global_position.direction_to(gun_tip.global_position) * 1500
var bounces = 0
points = []
var space_state = get_world_2d().direct_space_state
var result
var normal
var angle_to_normal
while bounces <= max_bounces:
points.append(starting_point)
result = space_state.intersect_ray(starting_point, end_point, [player])
if result:
normal = result.normal
angle_to_normal = (starting_point - end_point).angle_to(normal)
starting_point = result.position
end_point = normal.rotated(angle_to_normal) * diagonal
bounces -= 1
update()
func _draw() -> void:
for i in points.size() -1:
draw_line(i, i+1, Color(100,100,100), 5)