Godot 4中如何正确应用子弹在枪中的传播?

问题描述 投票:0回答:1

我正在为godot中的2D游戏制作各种枪支,它们都从一个名为arma.gd的脚本扩展,除了子弹传播之外一切都正常,它应该在子弹之间留下间隙,具体取决于弧度和bullet_qtd ,然而子弹之间的间隙几乎不存在。

大多数武器都是在玩家节点实例化的。 但子弹是由武器在世界节点中实例化的。

rn 类 arma.gd 像这样:

extends Node2D

class_name Arma

@export var bullet: PackedScene
@export var bullet_qtd: int = 2
@export_range(0, 360) var arc: float = 0
@export_range(0, 20) var fire_rate: float = 1.0
@onready var mundo = $"../../"   #world node
var can_shoot = true


func _ready():
    atirar()


func _physics_process(delta):
    look_at(get_global_mouse_position())


func atirar():
if can_shoot:
    can_shoot = false 
    for i in bullet_qtd:
        var new_bullet = bullet.instantiate()
        new_bullet.position = global_position
            
        if bullet_qtd == 1:
            new_bullet.rotation = global_rotation
                
        else:
            var arc_rad = deg_to_rad(arc)
            var increment = arc_rad / (bullet_qtd - 1)
            new_bullet.global_rotation = (
                (global_rotation - arc_rad / 2) + (increment * i)   
            )
                
        new_bullet.dir = global_rotation
        new_bullet.z_index = z_index
        mundo.add_child(new_bullet)
    await get_tree().create_timer(1 / fire_rate).timeout
    can_shoot = true
    atirar()

我已经尝试过更改度数、获取 new_bullet.rotation 的公式以及一些碰撞和遮罩,但似乎没有任何改变间隙。

项目符号类脚本:

extends CharacterBody2D

class_name Bullet

var zindex:= 1 as int
var dir: float

@export var bullet_velocidade: float
@export var bullet_penetracao:= PlayerVariaveis.penetracao as int
@export var bullet_dano: int
@onready var bullet_vida:= $bullet_vida as Timer


func _ready():
    z_index = zindex
    
    
func _physics_process(delta):
    position += (Vector2.RIGHT*bullet_velocidade).rotated(dir) * delta

我没有执行 move_and_colide 或 move_and_slide 的原因是,当子弹扩展 Area2D 而不是 CharacterBody2D 时我开始执行此操作。但如果你们认为重做剧本更好,我会做的。

“普通”子弹:“目前只有这个,但它们将始终从 Bullet 类扩展。”

extends Bullet

@onready var dano = bullet_dano * PlayerVariaveis.dano
@onready var velocidade = bullet_velocidade * PlayerVariaveis.velocidade


func _on_bullet_vida_timeout():
    queue_free()


func _on_area_2d_body_entered(body):
    if body is Inimigos:
        bullet_penetracao -= 1
        if penetracao <= 0:
            queue_free()
        body._on_hitbox_body_entered(self)
game-development game-physics godot godot4
1个回答
0
投票

受@BenderBoy启发的解决方案:

确实有效,只是结果发现我没有通过

new_bullet.dir
中修改后的旋转:

func atirar():
    if can_shoot:
        can_shoot = false 
        for i in bullet_qtd:
            var new_bullet = bullet.instantiate()
            new_bullet.position = global_position
            
            if bullet_qtd == 1:
                new_bullet.rotation = global_rotation
                new_bullet.dir = global_rotation
                
            else:
                var arc_rad = deg_to_rad(arc)
                var increment = arc_rad / (bullet_qtd - 1)
                var new_rotation = (global_rotation - arc_rad / 2) + (increment * i)    
                new_bullet.global_rotation = new_rotation
                new_bullet.dir = new_rotation
            
            new_bullet.zindex = z_index
            mundo.add_child(new_bullet)
        await get_tree().create_timer(1 / fire_rate).timeout
        can_shoot = true
        atirar()

此答案作为问题的编辑发布:如何在godot 4中正确应用子弹在枪中的传播? [已修复] 由 OP SDG 根据 CC BY-SA 4.0 进行。

© www.soinside.com 2019 - 2024. All rights reserved.