Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Get the rotation of the invisible point - rad2deg converts from Radians to Degrees
- var rot_point = round( rad2deg( get_node("Path2D/PathFollow2D").get_rot() ) )
- # Get the current rotation of the sprite
- var rot_sprite =round( rad2deg( get_node("Sprite").get_rot() ) )
- # Prevent flickering between -180 and 180
- if rot_point == -180:
- rot_point = 180
- # Get the shortest rotation direction (clockwise or counterclockwise)
- var a = abs(rot_sprite - rot_point)
- var b = 360 - abs(rot_sprite - rot_point)
- # Prevent b from falling back below zero
- if b < 0:
- b += 360
- a -= 360
- # Speed you want to rotate the sprite - you should move this to your var block at the top
- var rot_speed = 8
- # Only rotate, if a difference is given
- if a > 1:
- if a > b + rot_speed:
- get_node("Sprite").set_rot( deg2rad( rad2deg( get_node("Sprite").get_rot() ) + rot_speed ) )
- if a < b - rot_speed:
- get_node("Sprite").set_rot( deg2rad( rad2deg( get_node("Sprite").get_rot() ) - rot_speed ) )
- Finished code
- extends Node2D
- var speed = 100 # speed of the moving sprite
- var rot_speed = 8 # Speed you want to rotate the sprite
- var path = [] # array for all the points
- func _ready():
- # create a curve
- var curve = Curve2D.new()
- # now add your points - 1 point isn't enough...
- path.append( Vector2(200,200) )
- path.append( Vector2(0,200) )
- path.append( Vector2(200,0) )
- path.append( Vector2(400,400) )
- path.append( Vector2(0,0) )
- if path.size() > 0:
- for point in path:
- curve.add_point( point )
- get_node("Path2D").set_curve(curve)
- # Draw the path
- get_node("PathDraw").path = path
- get_node("PathDraw").update()
- # Initialization here
- set_fixed_process(true)
- pass
- func _fixed_process(delta):
- #print( get_node("Path2D").get_curve().get_point_count() )
- # Only 1 point... no movement required
- if get_node("Path2D").get_curve().get_point_count() < 2:
- set_fixed_process(false)
- return
- var n_offset = get_node("Path2D/PathFollow2D").get_offset() + delta * speed
- # move the point on the path
- get_node("Path2D/PathFollow2D").set_offset( n_offset )
- # set the sprite pos to the point pos
- get_node("Sprite").set_pos( get_node("Path2D/PathFollow2D").get_pos() )
- # Rotation:
- # Get the rotation of the invisible point - rad2deg converts from Radians to Degrees
- var rot_point = round( rad2deg( get_node("Path2D/PathFollow2D").get_rot() ) )
- # Get the current rotation of the sprite
- var rot_sprite =round( rad2deg( get_node("Sprite").get_rot() ) )
- # Prevent flickering between -180 and 180
- if rot_point == -180:
- rot_point = 180
- # Get the shortest rotation direction (clockwise or counterclockwise)
- var a = abs(rot_sprite - rot_point)
- var b = 360 - abs(rot_sprite - rot_point)
- # Prevent b from falling back below zero
- if b < 0:
- b += 360
- a -= 360
- # Only rotate, if a difference is given
- if a > 1:
- if a > b + rot_speed:
- get_node("Sprite").set_rot( deg2rad( rad2deg( get_node("Sprite").get_rot() ) + rot_speed ) )
- if a < b - rot_speed:
- get_node("Sprite").set_rot( deg2rad( rad2deg( get_node("Sprite").get_rot() ) - rot_speed ) )
- # alternative: set the sprite rotation to the point rotation
- # get_node("Sprite").set_rot( get_node("Path2D/PathFollow2D").get_rot() )
- # goal reached
- if get_node("Path2D/PathFollow2D").get_unit_offset() >= 1:
- set_fixed_process(false)
- extends Node2D
- var path = [] # path from the main script
- func _draw():
- var col = Color(255,0,0,1)
- # for each path point
- for i in range( path.size() ):
- if i > 0:
- draw_line( path[i-1], path[i], col )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement