AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题

问题[godot](coding)

Martin Hope
Moustic_Bonjour
Asked: 2025-01-22 22:28:10 +0800 CST

为什么我的坐标线没有在光标所在的位置结束?

  • 5

我试图在光标处结束一行。但是没有成功。

窗口化:
窗口化

全屏:
全屏显示

如您所见,蓝线并未在光标处结束。

这是我的代码:

extends Line2D

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
    #gets parent's position
    var parent := get_parent()
    
    #sets 2 points for the line
    add_point(parent.position)
    add_point(parent.position)



# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
    #deletes last position (cursor position)
    remove_point(1)
    
    #calculates the new point position
    var mousePos := get_global_mouse_position() #gets mouse position
    var adjust   := get_viewport_rect().size    #gets screen
    var deltaPos  = mousePos - (adjust/2)       #Adjust the coordinate (Point's (0,0) is in the middle of the screen while cursor's is on the top left corner)
    add_point(Vector2(deltaPos))
    print(deltaPos, points[1])

这条线是 Area 2d 的子元素,就像这样(它有一个 2d 精灵作为子元素,对应正方形中的建筑物。以下是所有构建

每次调用函数“_process”时,我都会打印鼠标坐标,以及应该位于光标上的结束线点坐标。您可以在窗口图像上看到这些坐标是相同的

一开始,差异就更加明显。之前:

前

这是因为鼠标的坐标和点的坐标原点不同(第一个是左上角,第二个是屏幕中心)。

godot
  • 1 个回答
  • 23 Views
Martin Hope
KuwinnLifestyle
Asked: 2024-12-31 20:26:32 +0800 CST

如何暂时禁用 Godot 中 RigidBody 上的物理碰撞?

  • 5

我对 Stack Overflow 和游戏开发都很陌生,如果这个问题很愚蠢,请见谅。我目前正在 godot 中开发一款游戏,玩家可以向敌人发射射弹(过于简单)。但是,我遇到了以下问题:射弹在玩家模型内部生成,然后与发射它的玩家发生碰撞并弹开。我的射弹使用 RigidBody2D 类型,而玩家是 CharacterBody2D。

我尝试了以下方法:

将碰撞图层蒙版设置为与玩家不同的图层

问题:射弹应该从墙壁反弹,然后击中玩家。因此,两者必须发生碰撞。

执行第一个建议,但在第一次墙壁反弹后启用遮罩层

问题:稍后我想使用类似于导弹的东西。玩家可以发射导弹,导弹会跟随鼠标移动,然后击中敌人或玩家。因此,导弹应该能够与玩家发生碰撞,而不会反弹到墙上。

执行第一个建议,但在 x 毫秒后启用遮罩层

问题:射弹的速度不同,并且玩家可以拥有不同大小的模型,所以我想避免这样的事情。

以偏移量生成射弹,使其从玩家命中框之外开始

问题:我试过这个,但看起来不太对劲,尤其是在玩家模型不同的情况下。但如果没有其他解决方案,这可能是可行的方法。

基本上,我想禁用两者之间的碰撞,然后在射弹离开玩家碰撞区时触发 body_exited 信号,然后重新启用碰撞。问题是,如果我禁用碰撞,body_exited 信号将不会触发,因为没有碰撞。但是启用碰撞后,射弹会从玩家身上弹开。基本上,我想要的是两者之间最初没有物理交互,但有主动碰撞检测。任何帮助都将不胜感激。此外,因为我是新手,所以非常感谢最佳实践建议。如果我的做法被认为是不好的做法,请告诉我。

godot
  • 1 个回答
  • 32 Views
Martin Hope
Frasher Gray
Asked: 2024-12-13 11:47:51 +0800 CST

await 后恢复函数 'beginRound()',但脚本已消失。Godot Await 错误

  • 6

await我遇到了在循环内多次触发的错误。

代码应该循环遍历所有帝国,告诉他们轮流行动,然后await在触发下一个帝国之前完成任务。这适用于第一轮(玩家的轮次),但当 AI 尝试调用时endTurn(),我收到错误:

E 0:01:08:0911   Map.gd:419 @ endTurn(): Resumed function 'beginRound()' after await, but script is gone.  
    At script: res://scripts/HostGameMenu.gd:197  
    <C++ Error>    Method/function failed. Returning: Variant()  
    <C++ Source>   modules/gdscript/gdscript_function.cpp:197 @ resume()  
    <Stack Trace>  Map.gd:419 @ endTurn()  
    Tribe.gd:221 @ takeTurn()  
    Map.gd:414 @ beginNewTurn()  
    HostGameMenu.gd:196 @ beginRound()  
    Map.gd:419 @ endTurn()  
    Map.gd:1912 @ endEmpireTurn()  

脚本一功能:

func beginRound():
    for i in range(empireTurnOrder.size()):
        empireTurnIndex = i #the only purpose this serves is being able to easily access whose turn it is for debugging from outside the for loop
        gameMap.beginNewTurn.rpc(empireTurnOrder[empireTurnIndex]) #empireTurnOrder is a PackedStringArray
        await gameMap.beginNextTurn
    gameMap.endRound.rpc() #gameMap points to the node containing Script Two

脚本二功能:

signal beginNextTurn

@rpc("any_peer", "call_local", "reliable")
func beginNewTurn(nextEmpire: String) -> void:
    var empire = getEmpire(nextEmpire)
    if empire.playerId != -1 and empire.playerId != userEmpire.playerId: #check is only for multiplayer, which is not the case for my tests. userEmpire is a reference to the empire node (node of Script Three) that the player controls
        pass
    else:
        if empire.playerId == userEmpire.playerId:
            #UI edits, irrelevant
        empire.takeTurn() #empire points to the node(s) of Script Three

func endTurn():
    if not serverHandler.inServer or multiplayer.get_unique_id() == 1: #serverHandler points to the node of Script One. inServer will always be true for singleplayer, which I am testing
        print(is_instance_valid(serverHandler))
        emit_signal("beginNextTurn")

func endRound():
    if not serverHandler.inServer or multiplayer.get_unique_id() == 1:
        serverHandler.beginRound()

func endEmpireTurn(): #button is tied to this function
    #edits UI a little
    endTurn()

脚本三功能:

func takeTurn():
    if playerId == -1:
        map.endTurn() #map points the node of Script Two
    else:
        #does nothing; button press will call endTurn for players

打印输出:

真
真

任何帮助都非常感谢。我发现关于此错误的唯一其他帖子是人们在等待_process()函数中的计时器,而这些问题的答案并不完全适用于我的情况。

编辑:

我在脚本一中添加了一个函数:

func turnEnded() -> void:
    print(empireTurnOrder[empireTurnIndex] + "'s turn is over.")

并将beginNextTurn信号连接到它,并且它也成功/正确地打印了信号的两次发射,但错误没有改变。

编辑2:

出于好奇想看看它是否有效,我更改了两个函数,beginNewTurn 和 beginRound,如下所示:

@rpc("any_peer", "call_local", "reliable")
func beginNewTurn(nextEmpire: String) -> bool:
    var empire = getEmpire(nextEmpire)
    if empire.playerId != -1 and empire.playerId != userEmpire.playerId:
        pass
    else:
        if empire.playerId == userEmpire.playerId:
            loadedData.get_node("ImportantMenuHub/EndTurn").set_disabled(false)
            loadedData.get_node("ResourceHUD/RaiseAll").set_disabled(false)
        empire.takeTurn()
    await beginNextTurn
    return true

func beginRound():
    for i in range(empireTurnOrder.size()):
        empireTurnIndex = i
        await gameMap.beginNewTurn(empireTurnOrder[empireTurnIndex])
    gameMap.endRound.rpc()

这会在行上给出错误“参数“函数”为空” emit_signal("beginNextTurn")。现在,显然,我确实希望我的解决方案与 rpc 函数调用兼容,但非常奇怪的是信号拒绝与beginRound()和 一起正确运行beginNewTurn()。顺便说一句,此输出仍然是

真
<帝国 1> 的回合结束了。
真
<帝国 2> 的回合结束了。

godot
  • 1 个回答
  • 33 Views
Martin Hope
Limer GG
Asked: 2024-11-27 01:23:07 +0800 CST

如何使用 godot 4 和 GDscript 使一个物体接另一个物体平滑旋转?

  • 5

我在场景中有一个坦克,炮塔作为子对象附加在上面。我在场景中还有一个使用脚本旋转的摄像头,我需要坦克炮塔随着摄像头的旋转而平稳旋转。

有人知道如何在摄像机后平稳旋转炮塔吗?这样当坦克转动时,炮塔保持在原位(因为摄像机不旋转)

我实现了一些类似的东西,但是当坦克旋转时,炮塔也会随之旋转,但由于摄像机此时不旋转,所以现在摄像机的旋转和炮塔之间有自由空间。

坦克场景树:https://i.imgur.com/BgwBz0q.png (为了执行独立于父对象的相机节点,我在相机节点脚本中使用此代码)

func _ready():
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
set_as_top_level(true)

我需要的旋转: https: //youtu.be/23IdDtPB4t0

godot
  • 1 个回答
  • 29 Views
Martin Hope
SPONGEBOB RED YT channel
Asked: 2024-11-19 04:33:26 +0800 CST

(GODOT 4.3)如何让通过图块地图移动的玩家在按下箭头键时永远移动?

  • 5

(我发过一个类似的帖子,只是我决定切换到 godot,这样我就可以比在 flash 中更轻松地制作游戏,因为它已经过时了)

我制作了一个玩家可以控制的角色,它通过瓷砖和瓷砖地图移动,我想让玩家在按下箭头键时可以永远移动(并移动到所述箭头键的方向),直到它进入接触不可行走瓷砖的可行走瓷砖

您可以给我任何有关如何操作的提示,以下是代码:

extends Sprite2D

class_name PacMan

@export var move_speed: float = 1



@onready var tile_map: TileMap = $"../TileMap"

@onready var sprite_2d: Sprite2D = $Sprite2D

@onready var animation_player: AnimationPlayer = $Sprite2D/AnimationPlayer


var is_moving = false

func _physics_process(delta: float) -> void:
    if is_moving == false:
        return
    if global_position == sprite_2d.global_position:
        is_moving = false
        return
    sprite_2d.global_position = sprite_2d.global_position.move_toward(global_position, move_speed)

func _ready() -> void:
    animation_player.play("chomping")

func _process(delta: float) -> void:
    if is_moving:
        return
    if Input.is_action_pressed("up"):
        rotation_degrees = 90
        move(Vector2.UP)
        is_moving = true
    elif Input.is_action_pressed("down"):
        rotation_degrees = 270
        move(Vector2.DOWN)
        is_moving = true
    elif Input.is_action_pressed("left"):
        rotation_degrees = 0
        move(Vector2.LEFT)
        is_moving = true
    elif Input.is_action_pressed("right"):
        rotation_degrees = 180
        move(Vector2.RIGHT)
        is_moving = true

func move(direction: Vector2):
    var current_tile: Vector2i = tile_map.local_to_map(global_position)
    var target_tile: Vector2i = Vector2i(
        current_tile.x + direction.x,
        current_tile.y + direction.y,
    )
    var tile_data: TileData = tile_map.get_cell_tile_data(0, target_tile)
    if tile_data.get_custom_data("walk_tiles") == false:
        return
    #Player movement
    is_moving = true
    global_position = tile_map.map_to_local(target_tile)
    sprite_2d.global_position = tile_map.map_to_local(current_tile)
godot
  • 1 个回答
  • 23 Views
Martin Hope
Sin nombre
Asked: 2024-10-05 02:14:21 +0800 CST

无效调用。基础“GDScript”中不存在函数“instantiate”

  • 5

启动游戏时出现此错误。有什么方法可以解决这个问题吗?

extends Node2D

var coin = preload("res://coin.gd")

func _on_timer_timeout():
    var coinTemp = coin.instantiate()   
    var generator = RandomNumberGenerator.new()
    var genint = randi_range(50, 500)
    coinTemp.position = Vector2 (genint,560) 
    add_child(coinTemp)

我找不到任何关于如何解决这个问题的信息

godot
  • 1 个回答
  • 15 Views
Martin Hope
M. R. M.
Asked: 2024-08-15 02:17:01 +0800 CST

如何根据给定的位置、旋转和比例计算 Transform3D?

  • 5

我试图在 2D 环境中模拟 3D 变换,因此我尝试自己完成 3D 部分的计算

为了参考,我使用了 Node3D 来检查我的计算方法是否正确

func calc_transform_3d(position_3d:Vector3, rotation_3d:Vector3, scale_3d:Vector3 ) -> Transform3D:
    
    var transform_3d := Transform3D(
        Basis.from_euler(rotation_3d).scaled(scale_3d),
        position_3d
    )
    
    return transform_3d

func _ready():
    var node_3d:Node3D = $Node3D
    var custom_transform := calc_transform_3d(node_3d.position, node_3d.rotation, node_3d.scale)
    
    print(custom_transform == node_3d.transform) # returns false

但是该calc_transform_3d()方法给出了错误的输出(仅供参考,我检查过这不是浮点错误)

那么仅给定和position,如何计算 Transform3D(类似于 Node3D 的计算方式)?rotationscale

godot
  • 1 个回答
  • 15 Views
Martin Hope
Simas Burdenkovas
Asked: 2024-07-26 20:51:16 +0800 CST

为什么 intersect_ray 说在 godot 4.2.2 中参数太多了?

  • 5

我是 Godot 的新手,目前正在尝试使用命中武器等,并使用intersect_ray()

当我尝试使用编写代码时,Intersect_ray()我不断收到一条错误消息“intersect_ray()”调用的参数太多。最多需要 1 个,但收到 2 个。”我不知道我做错了什么?以下是代码: var collision = bullet.intersect_ray(muzle.transform.origin, aimcast.get_collision_point

如果有帮助,我正在使用教程:https://www.youtube.com/watch?v =4jbfIN4t83k&list=PLJJ-tyPiN1L_68DjYLfn7c1Yq8Kx6NWPw&index=7

godot
  • 1 个回答
  • 19 Views
Martin Hope
PIXEL
Asked: 2024-03-30 21:55:12 +0800 CST

当换上另一名球员时,球员会冻结在位置上

  • 5

我有字符代码:

extends CharacterBody3D
@onready var camera_mount = $camera_mount
@onready var animation_player = $visuals/mixamo_base/AnimationPlayer
@onready var visuals = $visuals
@onready var camera_3d = $camera_mount/Camera3D

@export var is_current:bool = true
@export var is_main = true
@export var SPEED = 5.0
@export var JUMP_VELOCITY = 4.5
@export var SENSIVITY = 0.05
var walk_speed = 3.0
var run_speed = 5.0
var is_running = false
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")

func _ready() -> void:
    if is_main:
        Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)

func _input(event):
    if is_main:
        if event is InputEventMouseMotion:
            rotate_y(deg_to_rad(-event.relative.x*SENSIVITY))
            visuals.rotate_y(deg_to_rad(event.relative.x*SENSIVITY))
            camera_mount.rotate_x(deg_to_rad(-event.relative.y*SENSIVITY))
            

        
func _physics_process(delta):
    if is_main:
        camera_3d.current = is_current

        if not is_on_floor():
            velocity.y -= gravity * delta
        if Input.is_action_pressed("run"):
            SPEED = run_speed
            is_running = true
        else:
            is_running = false
            SPEED = walk_speed
            

        if Input.is_action_just_pressed("ui_accept") and is_on_floor():
            velocity.y = JUMP_VELOCITY
            
        if Input.is_action_just_pressed("ui_cancel"):
            Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)


        var input_dir = Input.get_vector("move_left", "move_right", "move_forward", "move_back")
        
        var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
        if direction:
            if animation_player.current_animation != "walking" && is_running == false:
                animation_player.play("walking")
                
            if animation_player.current_animation != "running" && is_running:
                animation_player.play("running")
            
                
            visuals.look_at(position+direction)
                
            velocity.x = direction.x * SPEED
            velocity.z = direction.z * SPEED
        else:
            if animation_player.current_animation != "idle":
                animation_player.play("idle")
                
            velocity.x = move_toward(velocity.x, 0, SPEED)
            velocity.z = move_toward(velocity.z, 0, SPEED)
            
        camera_mount.rotation.x = clamp(camera_mount.rotation.x, 
            deg_to_rad(-90), 
            deg_to_rad(70)
        )

        move_and_slide()

负责使用按钮更改游戏中角色的脚本:

extends Node3D

@onready var player_1 = $"../Player1"
@onready var player_2 = $"../Player2"

func _physics_process(_delta):
    if Input.is_action_just_pressed("switch_player"):
        if player_1.is_main:
            player_1.is_main = false
            player_2.is_main = true
            player_2.is_current = true
            player_1.animation_player.play("idle")
        else:
            player_1.is_main = true
            player_2.is_main = false
            player_1.is_current = true
            player_2.animation_player.play("idle")

问题是,如果你在你之前效力的球员在空中时更换球员,那么他就会冻结在这个位置上。我怎样才能做到,一旦我换了一名球员,我之前效力的那个球员就倒地,即开始屈服于物理定律?

godot
  • 1 个回答
  • 22 Views
Martin Hope
Willien
Asked: 2024-03-09 01:48:04 +0800 CST

全局变量未在全局脚本之外更新

  • 5

是的,当我开始游戏时,球正在进入(并离开)2D区域

是的global.gd,是一个自动加载的脚本

在全局中,我有一个很大的area2d来检测是否有人进入。 var chase_ball全局内部正在更新,但在Global.chase_ball其始终为 false (默认值)

最初area2D位于敌人节点内部,但我希望area2D固定在主场景中,而不是与敌人一起移动

全局.gd:

extends Node2D

var chase_ball = false

func _process(delta):
        print(chase_ball)

func _on_ball_detector_body_entered(body):
    chase_ball = true

func _on_ball_detector_body_exited(body):
    chase_ball = false

输出:

true
false
true
false
(same loop as above)

敌人.gd:

func _process(delta):
    print(Global.chase_ball)

输出:

false
false
false
false
(same loop as above)

预期结果是Global.chase_ball根据func _on_ball_detector_body_entered/exited(body):

godot
  • 1 个回答
  • 21 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve