How to slow down an object after shooting it

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Martogh

So I’ve made a 2d plat form game, for it i need to make an ability which shoots at a fast rate, slows down and then hovers above the ground. I am new to coding in Godot and have only made the object shoot in one direction but it doesn’t slow down or hover in one spot. How could i possibly make the ability shoot forwards slow down then hover in one spot for a few seconds?

-Sorry if this is not worded very well.

:bust_in_silhouette: Reply From: avencherus

An easy way to do this is to change the Engine.time_scale temporarily using timers. You would also want to be careful to cancel out the scaling to the time used in some way, so it isn’t also slowed down.

That can be done by multiplying the scale into the duration used, or dividing the scaling out of the delta if you’re using processing.

A basic example looks like this. This will slow time to 30% for 1 second for pressing any button. The timer refreshes for multiple presses.

You can also get fancier results by using easing on the value used for scaling to have it ramp up or down over time.

extends Node2D


const SCALE = 0.3
const DURATION = 1.0


func _input(e):
	if(e.is_pressed()):
		time_slow(DURATION, SCALE)


var time_remaining = 0.0

func time_slow(duration, amount):

	Engine.time_scale = amount

	time_remaining = duration
	set_physics_process(true)



func _ready():
	set_physics_process(false)

func _physics_process(delta):

	if(time_remaining > 0.0):
		time_remaining -= delta / Engine.time_scale

		print(time_remaining)

		if(time_remaining <= 0.0):

			Engine.time_scale = 1.0
			set_physics_process(false)

Thanks a lot for the help.

Martogh | 2020-07-19 02:09