0 votes

Hello I am practicing in godot at the same time that I practiced C # I need to make a shooting mechanic similar to this one:

enter image description here

But I do not achieve the effective way to do it, try to create the instance in a 2D position that is in the player and put the laser / bullet gravity up and not operated

Do you have any idea how to do it?

Player script:

using Godot;
using System;

public class Player : Area2D
{
        [Signal]
        public delegate void Golpe();

[Export]
public int velocidad;

[Export]
public int limite;

[Export]
public PackedScene Bala;

[Export]
public int bala_vel;

private Vector2 _screenSize;

public override void _Ready()
{
    _screenSize = GetViewport().GetSize();
    Hide();
}

public override void _Process(float delta)
{
    Vector2 movimiento = new Vector2();
    Particles2D fuego = (Particles2D)GetNode("ParticulasFuego");


    if (Input.IsActionPressed("ui_right"))
    {
        movimiento.x += 1;
        fuego.ProcessMaterial.Set("angular_velocity", 8);
    }
    if (Input.IsActionPressed("ui_left"))
    {
        movimiento.x -= 1;
        fuego.ProcessMaterial.Set("angular_velocity", -8);
    }

    if (Input.IsActionPressed("ui_accept"))
    {
        RigidBody2D instanciaBala = (RigidBody2D)Bala.Instance();
        RayCast2D balaContainer = (RayCast2D)GetNode("BalaContainer");
        balaContainer.AddChild(instanciaBala);

        Position2D positionBala = (Position2D)GetNode("PositionBala");
        instanciaBala.Position = positionBala.Position;

        //float direction = positionBala.Rotation + Mathf.Deg2Rad(0);
        //instanciaBala.Rotation = direction;

        //instanciaBala.SetLinearVelocity(new Vector2(0, 500f).Rotated(direction));
    }

    if (movimiento.Length() > 0)
    {
        movimiento = movimiento.Normalized() * velocidad;
    }
    else
    {
        fuego.ProcessMaterial.Set("angular_velocity", 0);
    }

    Position += movimiento * delta;
    Position = new Vector2(
        x: Mathf.Clamp(Position.x, limite, _screenSize.x),
        y: Mathf.Clamp(Position.y, limite, _screenSize.y)
    );
}

public void Start(Vector2 pos)
{
    CollisionPolygon2D collision = (CollisionPolygon2D)GetNode("CollisionPlayer");

    Position = pos;
    Show();
    collision.Disabled = false;
}

public void OnPlayerBodyEntered(PhysicsBody2D body)
{
    CollisionPolygon2D collision = (CollisionPolygon2D)GetNode("CollisionPlayer");

    Hide();
    EmitSignal("Golpe");
    collision.Disabled = true;
}
}

If there is something wrong written I'm sorry I do not speak English I translated it from Google

in Engine by (24 points)

1 Answer

0 votes

Something might be wrong with the way you instance the bullet: you are adding it as a child of a child of the player, which means its position will more or less be set relative to the player and will follow it. Usually, in shooter games, bullets are instanced outside of the player node, directly in the world. For example, as child of the parent of the player. This way, they can move independently.

by (28,738 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.