Launching player into the air

Godot Version

4.2.2

Question

So I am very new to godot and I was wondering how i would get a sort of area that when the player enters it, Launches them into the air in a sort of explosion way
thx in advance ^^

1 Like

Hello Azury, Do you mean when the character steps on a mine? If so? It’s not hard to write. You need to:

  1. Create a Mine Object.
  2. Attach a Script to it.

MineExpl

We will write in C# Since it is my native language :stuck_out_tongue:

1. Creating a mine
Let’s create a mine scene:

Create a new scene and add an Area3D to it.
Add a CollisionShape3D child node to the Area3D, which will define the area the character steps on. Use a simple shape, such as a CylinderShape3D or BoxShape3D.
Add a MeshInstance3D child node to render the mine (using a sphere, for example).

2. Writing a Script for a Mine
Now let’s add a script to Area3D that will track collisions and cause an explosion:

using Godot;
using System;

public partial class Mine : Area3D
{
    [Export] private float explosionRadius = 5.0f;  
    [Export] private float explosionForce = 10.0f;  
    [Export] private PackedScene explosionEffect;  

    public override void _Ready()
    {
        Connect("body_entered", new Callable(this, nameof(OnBodyEntered)));
    }

    private void OnBodyEntered(Node body)
    {
        if (body is CharacterBody3D)
        {
            Explode();
        }
    }

    private void Explode()
    {
        // Создаем эффект взрыва
        if (explosionEffect != null)
        {
            var explosionInstance = (Node3D)explosionEffect.Instance();
            explosionInstance.GlobalTransform = GlobalTransform;
            GetParent().AddChild(explosionInstance);
        }

        // Apply force to к Evironment Objects
        var spaceState = GetWorld3d().DirectSpaceState;
        var results = spaceState.IntersectSphere(GlobalTransform.origin, explosionRadius);

        foreach (var result in results)
        {
            if (result["collider"] is RigidBody3D rigidBody)
            {
                Vector3 forceDirection = rigidBody.GlobalTransform.origin - GlobalTransform.origin;
                rigidBody.ApplyImpulse(forceDirection.Normalized() * explosionForce, Vector3.Zero);
            }
        }

        // Remove the mine immediately 
        QueueFree();
    }
}

3. Adding a mine to the level
Insert your mine into the level (e.g. via a scene).
Make sure the character has a CharacterBody3D component so it can interact with the mine.

4. Setting up the explosion
explosionRadius — The radius in which objects will be affected by the explosion.
explosionForce — The force of the explosion that will be applied to objects within the radius.
explosionEffect — If you want to add a visual or sound effect, create a scene with an explosion effect and specify it in this field.

5. Finishing touches
Make sure the mine does not react to itself or other mines. This can be done by adding a check for the object type in the OnBodyEntered method.
You can improve visual and audio effects by adding them to Explode().

6. An example of using the explosion effect
You can create a simple explosion effect, for example, by adding a temporary sphere with an animation of increasing the radius and then disappearing.

public partial class ExplosionEffect : Node3D
{
    public override void _Ready()
    {
        var animationPlayer = GetNode<AnimationPlayer>("AnimationPlayer");
        animationPlayer.Play("explosion");
    }

    private void OnAnimationFinished()
    {
        QueueFree();
    }
}

This is code must to be refactored and Debugged

1 Like