When i instantiate a scene, my game lags. How do i fix it?

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

In my game i’m want it so when my player dies, it instantiate a scene which contains rigid body’s that are in the shape of the player (a circle). But when i do it, the game lags for a second. how do I fix it?

This is my Code:

       Player:

using Godot;
using System;

public class Player : RigidBody2D
{

[Export] PackedScene DeathOfDeath =  ResourceLoader.Load("res://PreFabs/DeathOfPlayer.tscn") as PackedScene;
public override void _Ready()
{
    
}



public void _on_Area2D_body_entered(Node2D other) {
    if (other.IsInGroup("Can kill Player")) {
        KillPlayer();
    }
}


public void KillPlayer() {

    Node2D death = DeathOfDeath.Instance() as Node2D;
    death.SetPosition(Position);
    GetParent().AddChild(death);
    QueueFree();
}

}

If I understand this correctly, your player node spawns another node, and then the player node is freed? Is this for a death animation? If so, why not have the animation (or death node) already instantiated on the player node?

Ertain | 2020-07-03 18:35