how to "jitter" a position node2d

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By jjphung
:warning: Old Version Published before Godot 3 was released.

So what I want to do is to move a node2d’s position up and down couple of pixels constantly at intervals to create a jittering effect. What I have so far is a function “onMove” that gets called every 1 second. Or is there any other way of achieving this effect?

  func _ready():
        var viewport = get_viewport().get_rect().size
        x = viewport.x
        y = viewport.y
        set_global_pos(Vector2(x/2,y/4))

    	var timer = Timer.new()
    	timer.set_timer_process_mode(0)
    	timer.set_wait_time(1)
    	timer.connect("timeout", self, "onMove")
    	add_child(timer)
    	timer.start()
    
    	#Sets frame updates
    	set_fixed_process(true)
    	
    	#Sets UI
    	set_process(true)
    
    	#Set inputs 
    	set_process_input(true)
    	
    func onMove():
    	print("here")
:bust_in_silhouette: Reply From: Nuno Donato

Thats one way to do it, there are always different ways to do things. Others that come to my mind:

  • use an animation player
  • use a sin() function to offset the translation, you can use clamp() to make it more jittery and less smooth

Thank you! I’ve went with the animation player. So much cleaner.

jjphung | 2017-03-28 19:55