Advertisement
otorp2

create path and follow

Jul 15th, 2015
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. # Get the rotation of the invisible point - rad2deg converts from Radians to Degrees
  2. var rot_point = round( rad2deg( get_node("Path2D/PathFollow2D").get_rot() ) )
  3.  
  4. # Get the current rotation of the sprite
  5. var rot_sprite =round( rad2deg( get_node("Sprite").get_rot() ) )
  6.  
  7. # Prevent flickering between -180 and 180
  8. if rot_point == -180:
  9. rot_point = 180
  10.  
  11. # Get the shortest rotation direction (clockwise or counterclockwise)
  12. var a = abs(rot_sprite - rot_point)
  13. var b = 360 - abs(rot_sprite - rot_point)
  14.  
  15. # Prevent b from falling back below zero
  16. if b < 0:
  17. b += 360
  18. a -= 360
  19.  
  20. # Speed you want to rotate the sprite - you should move this to your var block at the top
  21. var rot_speed = 8
  22.  
  23. # Only rotate, if a difference is given
  24. if a > 1:
  25. if a > b + rot_speed:
  26. get_node("Sprite").set_rot( deg2rad( rad2deg( get_node("Sprite").get_rot() ) + rot_speed ) )
  27. if a < b - rot_speed:
  28. get_node("Sprite").set_rot( deg2rad( rad2deg( get_node("Sprite").get_rot() ) - rot_speed ) )
  29. Finished code
  30. extends Node2D
  31.  
  32. var speed = 100 # speed of the moving sprite
  33. var rot_speed = 8 # Speed you want to rotate the sprite
  34. var path = [] # array for all the points
  35.  
  36. func _ready():
  37.  
  38. # create a curve
  39. var curve = Curve2D.new()
  40.  
  41. # now add your points - 1 point isn't enough...
  42. path.append( Vector2(200,200) )
  43. path.append( Vector2(0,200) )
  44. path.append( Vector2(200,0) )
  45. path.append( Vector2(400,400) )
  46. path.append( Vector2(0,0) )
  47.  
  48. if path.size() > 0:
  49. for point in path:
  50. curve.add_point( point )
  51.  
  52. get_node("Path2D").set_curve(curve)
  53.  
  54. # Draw the path
  55. get_node("PathDraw").path = path
  56. get_node("PathDraw").update()
  57.  
  58. # Initialization here
  59. set_fixed_process(true)
  60. pass
  61.  
  62. func _fixed_process(delta):
  63. #print( get_node("Path2D").get_curve().get_point_count() )
  64.  
  65. # Only 1 point... no movement required
  66. if get_node("Path2D").get_curve().get_point_count() < 2:
  67. set_fixed_process(false)
  68. return
  69.  
  70. var n_offset = get_node("Path2D/PathFollow2D").get_offset() + delta * speed
  71.  
  72. # move the point on the path
  73. get_node("Path2D/PathFollow2D").set_offset( n_offset )
  74.  
  75. # set the sprite pos to the point pos
  76. get_node("Sprite").set_pos( get_node("Path2D/PathFollow2D").get_pos() )
  77.  
  78. # Rotation:
  79.  
  80. # Get the rotation of the invisible point - rad2deg converts from Radians to Degrees
  81. var rot_point = round( rad2deg( get_node("Path2D/PathFollow2D").get_rot() ) )
  82.  
  83. # Get the current rotation of the sprite
  84. var rot_sprite =round( rad2deg( get_node("Sprite").get_rot() ) )
  85.  
  86. # Prevent flickering between -180 and 180
  87. if rot_point == -180:
  88. rot_point = 180
  89.  
  90. # Get the shortest rotation direction (clockwise or counterclockwise)
  91. var a = abs(rot_sprite - rot_point)
  92. var b = 360 - abs(rot_sprite - rot_point)
  93.  
  94. # Prevent b from falling back below zero
  95. if b < 0:
  96. b += 360
  97. a -= 360
  98.  
  99. # Only rotate, if a difference is given
  100. if a > 1:
  101. if a > b + rot_speed:
  102. get_node("Sprite").set_rot( deg2rad( rad2deg( get_node("Sprite").get_rot() ) + rot_speed ) )
  103. if a < b - rot_speed:
  104. get_node("Sprite").set_rot( deg2rad( rad2deg( get_node("Sprite").get_rot() ) - rot_speed ) )
  105.  
  106. # alternative: set the sprite rotation to the point rotation
  107. # get_node("Sprite").set_rot( get_node("Path2D/PathFollow2D").get_rot() )
  108.  
  109. # goal reached
  110. if get_node("Path2D/PathFollow2D").get_unit_offset() >= 1:
  111. set_fixed_process(false)
  112. extends Node2D
  113.  
  114. var path = [] # path from the main script
  115.  
  116. func _draw():
  117. var col = Color(255,0,0,1)
  118.  
  119. # for each path point
  120. for i in range( path.size() ):
  121. if i > 0:
  122. draw_line( path[i-1], path[i], col )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement