English 中文(简体)
与RyCast3d在Adot4.2.1中发现参与者的问题
原标题:Issue with RayCast3d with detecting player in godot 4.2.1

我正在从事一个敌人的工作,该敌人使用简单的射线探测器,以发现参与者在他们面前,然后开始追捕参与者。 然而,我还是在运行时发现这一错误:“要求停职的团体是无主的。

该法典:

    extends CharacterBody3D
    
    var player = null
    
    var health = 100
    
    const SPEED = 4.0
    const ATTACK_RANGE = 2.0

    @export var player_path : NodePath
    @onready var nav_agent = $NavigationAgent3D
    @onready var view_ray = $MeshInstance3D/RayCast3D

    var player_found = false

    # Called when the node enters the scene tree for the first time.
    func _ready():
    player = get_node(player_path)
    
    
    # Called every frame.  delta  is the elapsed time since the previous frame.
    func _process(delta):
    velocity = Vector3.ZERO
    
    if(player_found == true):
        nav_agent.set_target_position(player.global_transform.origin)
        var next_nav_point = nav_agent.get_next_path_position()
        velocity = (next_nav_point - global_transform.origin).normalized() * SPEED
    
        look_at(Vector3(player.global_position.x, global_position.y, player.global_position.z), Vector3.UP)
    
    if(player_found == false):
        pass
    
    if view_ray.get_collider().is_in_group("Player") and view_ray.get_collider() != null:
            player_found = true
    
    if view_ray.get_collider() == null:
        player_found = false
    
    move_and_slide()


    func _target_in_range():
    return global_position.distance_to(player.global_position) < ATTACK_RANGE


    func _hit_finished():
    if global_position.distance_to(player.global_position) < ATTACK_RANGE + 1.0:
        var dir = global_position.direction_to(player.global_position)
        player.hit(dir)

    func hit():
    health -= 33
    if (health == 0 or health < 0):
        queue_free()

这里的结构是:

VC_scout
  MeshInstance3D
    RayCast3D
  CollisionShape3D
  NavigationAgent3D

事先感谢你!

问题回答

这一错误告诉你,你正在打电话is_in_groupnull。 自您打电话is_in_group以来,该守则的这一部分:

    if view_ray.get_collider().is_in_group("Player") and view_ray.get_collider() != null:
            player_found = true

我们必须得出结论,<代码>view_ray.get_collider()为null。 这就意味着,光线是 t。

是的,你正在检查<代码>view_ray.get_collider()!= 无<>代码,但你正在检查AFTER,你试图打电话is_in_group

Thus, you should be able to solve this swapping the checks:

    if view_ray.get_collider() != null and view_ray.get_collider().is_in_group("Player"):
            player_found = true

由于我们有<条码>和,<条码>和<条码>仅限<条码>。 条件为<代码>true...... 如果其中一项条件为<代码>false,那么我们知道结果将是false而无需核对。 因此,在<代码>view_ray.get_collider() ! = 无时,fal(即:view_ray.get_collider() 戈麦斯可检测<代码>view_ray.get_collider().is_in_group(“Player”)。

To be completely clear, Godot will evaluate the bool conditions from left to right, and will short-circuit (i.e. exit early) if possible. This is a common feature in many programming languages, see Short-circuit evaluation.





相关问题
Better solution to a ton of switch cases in command line game

I’m currently writing a project in c++ to turn the book “Fighting Fantasy” into a command line game. For those of you who don’t know, Fighting Fantasy is an interactive novel game where you start on ...

Issue creating game global code organisation

I am recreating a Pokemon game in C++ but have issue with organisation of code. I don t know what to put in class and what to put in struct. In my code Pokemon and Attaque are 2 different classes ...

Decoding NovaPack (NPK) binary format

I am trying to extract & replace some of the assets in the Mac game "Luxor 3" with my own. Inside the Contents/Resources folder there is a big file called data.npk. Opening it up in a hex editor, ...

Gaming in Facebook for Android

I ve developed a Facebook application for Android. For further development, I would like to know whether I can enhance it to add gaming features of Facebook in it. Is there a flash player that I can ...

JSON Spawning mysql sleep processes

I am in the process of making a Javascript(Front end, PHP back end) game. In this game it checks the server for updates every 2 seconds. There is one 1 sql call being run and at the end I use $...

热门标签