鱼C论坛

 找回密码
 立即注册
查看: 52|回复: 1

godot再次求助

[复制链接]
发表于 前天 13:14 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
godot再次求助
  1. extends Area2D

  2. @onready var launcher: CharacterBody2D = get_node("/root/Main/Player")

  3. @onready var speed : float = launcher.bullet_speed
  4. @onready var rate : float = launcher.bullet_rate
  5. @onready var max_distance : float= launcher.bullet_distance
  6. @onready var attack : int = launcher.attack

  7. var bullets = []
  8. var distances = []
  9. var scene = preload("res://bullet/bullet.tscn")
  10. var is_firing := false

  11. func _process(delta: float) -> void:
  12.         for each in bullets:
  13.                 var direction = Vector2.UP.rotated(each[0].global_rotation).normalized()
  14.                 var velocity = direction * speed * delta
  15.                 each[0].global_position += velocity
  16.                
  17.                 # 判断是否到了最大距离
  18.                 if each[1].distance_to(each[0].global_position) >= max_distance:
  19.                         each[0].queue_free()
  20.                         bullets.erase(each)
  21.                
  22. func _input(event: InputEvent) -> void:
  23.         if event.is_action_pressed("fire"):
  24.                 await _fire_bullet()
  25.                
  26. func _fire_bullet() -> void:
  27.         if is_firing:
  28.                 return
  29.        
  30.         is_firing = true
  31.         _create_bullet()
  32.         await get_tree().create_timer(rate).timeout
  33.         is_firing = false

  34. func _create_bullet() -> void:
  35.         var ins = scene.instantiate()
  36.         self.add_child(ins)
  37.         ins.global_position = launcher.global_position
  38.         ins.global_rotation = launcher.global_rotation
  39.        
  40.         var list = [ins, ins.global_position]
  41.         bullets.append(list)
复制代码

老是冒出几个飞得很快的子弹
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 前天 13:15 | 显示全部楼层
问题分析及解决方案:
你的子弹速度异常可能是由delta时间处理不当和节点坐标计算错误导致的。以下是具体修复步骤:

1. 修正子弹移动逻辑
将_process函数中的移动计算改为:

  1. func _process(delta: float) -> void:
  2.     for each in bullets:
  3.         var direction = Vector2.UP.rotated(each.global_rotation)
  4.         each.global_position += direction * speed * delta  # delta应只乘一次

  5.         # 记录初始位置用于距离检测
  6.         if not distances.has(each):
  7.             distances[each] = each.global_position
  8.         elif distances[each].distance_to(each.global_position) >= max_distance:
  9.             each.queue_free()
  10.             bullets.erase(each)
  11.             distances.erase(each)
复制代码


2. 初始化变量
修正缺失的数组初始化:

  1. var bullets = []
  2. var distances = {}
复制代码


3. 修正子弹生成
修改_create_bullet函数:

  1. func _create_bullet() -> void:
  2.     var ins = scene.instantiate()
  3.     add_child(ins)
  4.     ins.global_position = launcher.global_position
  5.     ins.global_rotation = launcher.global_rotation
  6.     bullets.append(ins)  # 直接添加实例到数组
复制代码


4. 检查玩家属性值
确认Player节点的这些属性值是否合理:
bullet_speed 建议初始值设为200-400
bullet_rate 建议设为0.1-0.3秒

常见问题排查:
  • 如果子弹呈放射状乱飞,检查玩家节点的rotation属性是否正确
  • 速度仍然异常时,在Player脚本中print输出bullet_speed值
  • 确保所有子弹实例都正确添加到bullets数组


建议先尝试这些修改,如果问题仍然存在可以提供更多关于Player节点的属性设置信息。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-20 20:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表