Skip to content

Instantly share code, notes, and snippets.

@Razzlegames
Last active June 29, 2019 20:03
Show Gist options
  • Select an option

  • Save Razzlegames/de9e6595a9ed379a1b3bac1a310f9d98 to your computer and use it in GitHub Desktop.

Select an option

Save Razzlegames/de9e6595a9ed379a1b3bac1a310f9d98 to your computer and use it in GitHub Desktop.
movingPlatformBehavior.gd
extends Node2D
var path_direction = 1
var pathFollow2D = null
export var curve_speed = 80
# Path to follow
export(NodePath) var pathFollow2DNodePath = null
func _ready():
if(pathFollow2DNodePath != null):
pathFollow2D = get_node(pathFollow2DNodePath)
else:
pathFollow2D = get_parent()
if (!pathFollow2D is PathFollow2D):
pathFollow2D = null
turnOffProcessSinceWillNeverMove()
func turnOffProcessSinceWillNeverMove():
set_process(false)
func _physics_process(delta):
moveOnPath(delta)
func moveOnPath(delta):
if(pathFollow2D != null):
var prev_speed = curve_speed * path_direction
if(pathFollow2D.get_unit_offset() >= 1.0 && path_direction == 1):
path_direction = -1
elif(pathFollow2D.get_unit_offset() <= 0.0 && path_direction == -1):
path_direction = 1
var speed = lerp(prev_speed, curve_speed*path_direction, delta/5)
# Slow down near pathFollow2D edges to avoid throwing hero/etc in the air
if(pathFollow2D.get_unit_offset() <= 0.33 || pathFollow2D.get_unit_offset() >= 0.77):
speed /= 1.4
pathFollow2D.set_offset(pathFollow2D.get_offset() + speed * delta)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment