Skip to content

Instantly share code, notes, and snippets.

@ultimateprogramer
Last active January 17, 2021 12:27
Show Gist options
  • Select an option

  • Save ultimateprogramer/0a73e6e4b14cdd31898b873358ac4137 to your computer and use it in GitHub Desktop.

Select an option

Save ultimateprogramer/0a73e6e4b14cdd31898b873358ac4137 to your computer and use it in GitHub Desktop.
GDScript Helper for Flipping Physics Objects - Godot 3
extends Object
var flip_h = false
var flip_v = false
var node = null
func _init(node):
self.node = node
func get_flip_h():
return flip_h
func set_flip_h(enable):
if enable == flip_h:
return
flip_h = enable
h_flip_children()
func get_flip_v():
return flip_v
func set_flip_v(enable):
if enable == flip_v:
return
flip_v = enable
v_flip_children()
func h_flip_children():
for n in node.get_children():
if not (n is Node2D):
continue
if n is CollisionShape2D or n is CollisionObject2D:
n.rotation = -2.0 * n.rotation
else:
if flip_h:
n.scale = Vector2(-1, 1)
else:
n.scale = Vector2(1, 1)
var pos = n.position
n.translate(Vector2(-2.0 * pos.x, 0.0))
func v_flip_children():
for n in node.get_children():
if not (n is Node2D):
continue
if n is CollisionShape2D or n is CollisionObject2D:
n.rotation = -2.0 * n.rotation
else:
if flip_h:
n.scale = Vector2(1, -1)
else:
n.scale = Vector2(1, 1)
var pos = n.position
n.translate(Vector2(0.0, -2.0 * pos.y))
extends KinematicBody2D
const FLIP_HELPERS = preload("res://scripts/flip_helpers.gd")
func _ready()
flip_helpers = FLIP_HELPERS.new(self)
# Invoking
func do_a_flip_h(do_flip):
flip_helpers.set_flip_h(do_flip)
func do_a_flip_v(do_flip):
flip_helpers.set_flip_v(do_flip)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment