'I am trying to set all rigidbodys' velocity to 0, then to what it was before

So I have been trying to create a pause menu for the past few hours. However, I cannot figure out how to stop the rigidbodies from moving. If there is a way to stop all rigidbodies at once, please tell me, if not, I can set it to each and every script with a rigid body. Here is my code so far:

extends Position3D
onready var charCamera = get_viewport().get_camera()
##var direction = Camera.global_transform.basis.get_euler()

signal spawned(spawn)

export(PackedScene) var spawnling_scene
var linear_velocity_on_pause = 0
var not_paused_anymore = false
var paused = false
#var Popup1 = self.get_parent().get_parent().get_parent().get_node("Popup")
func _physics_process(_delta):
    
    if self.get_parent().get_parent().get_parent().get_node("Popup").visible == false:
        if Input.is_action_pressed("leftClick"):
            spawn()
        if paused == true:
            not_paused_anymore = true
            paused = false
        
    if self.get_parent().get_parent().get_parent().get_node("Popup").visible == true:
        linear_velocity_on_pause = spawnling_scene.instance().linear_velocity
        paused = true
        spawnling_scene.instance().set_mode(1)
        spawnling_scene.instance().linear_velocity = get_parent().get_parent().get_parent().get_node("LinearVelocityOf0").linear_velocity
    
    if not_paused_anymore == true:
        spawnling_scene.instance().set_mode(0)
        
        spawnling_scene.instance().linear_velocity = linear_velocity_on_pause
        not_paused_anymore = false

func spawn():
    var spawnling = spawnling_scene.instance()
    spawnling.linear_velocity = charCamera.global_transform.basis.z * -100
    #spawnling.global_transform.basis = charCamera.global_transform.basis
    add_child(spawnling)
    spawnling.set_as_toplevel(true)
    emit_signal("spawned", spawnling)
    ##insert pause system
    return spawnling

##var spawnling = spawnling_scene.instance()
##  
##  add_child(spawnling)
##  spawnling.set_as_toplevel(true)


Solution 1:[1]

I'm not answering the question of how to set the velocity of all rigid bodies to zero.


If you want to make a pause menu, this is what you should know:

Godot has a puse system, which you can use like this to pause:

get_tree().paused = true

And to resume:

get_tree().paused = false

See Pausing Games.

Which Nodes gets to execute when get_tree().paused is set to true depend on their pause_mode property. By default they will all stop, but if you set their pause_mode to PAUSE_MODE_PROCESS they will continue to work when get_tree().paused is set to true. And that is what you want to do with the Node that make up your pause menu UI.


However, that system will not pause shaders. Their TIME will continue to tick. If you want to "freeze" shaders you can set a 0 to their time scaling like this:

VisualServer.set_shader_time_scale(0)

Set it to 1 for normal speed:

VisualServer.set_shader_time_scale(0)

And you can set other values to have them slow down or speed up.


Speaking of slow down and speed up. If you want to do that for the rest of the game (not just shaders), you can use Engine.time_scale. And if there is some timing that you don't want to be affected, you would have to write it using the time functions in the OS class.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Theraot