最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - BodyEnter and BodyExit signal not working, GetOverlappingBodies is working - Stack Overflow

programmeradmin3浏览0评论

I am trying to set up my game to detect when my player character (CharacterBody2D) is moving over an NPC (AnimatedSprite2D with Area2D underneath it). Both have a shape etc and the layers and the masks all have the first bit set.

I have connected the OnEnter(Node2D body) and OnExit(Node2D body) methods to the Area2d body_enter and body_exit signals respectively via the UI.

The issue is that my methods for the signals don't fire but when I print GetOverlappingBodies(), I can see my player appearing and disappearing again.

The Signals for my Area2d, which is called NPCArea:

The node tree of NPC:

The node tree of player:

The root node is a CharacterBody2d, which should be detected by the area.

public partial class NpcArea : Area2D
{

    // Called every frame. 'delta' is the elapsed time since the previous frame.
    public override void _Process(double delta)
    {
        if (Input.IsActionPressed("interact"))
        {
            var overlap = GetOverlappingBodies();
            GD.Print(overlap.Count);
            foreach (var node in overlap) {
                GD.Print(node.Name);
            }
        }
    }

    public void OnEnter(Node2D node) 
    {
        Debug.Print("something entered:");
        Debug.Print(node.Name);
        if (node.Name.ToString().StartsWith("Player"))
        {
            var parent = GetParent<Npc>();
            var bubble = parent.GetNode<BoxContainer>("OnEnterBubble");
            bubble.Visible = true;
        }
    }

    public void OnExit(Node2D node)
    {
        Debug.Print("something exited:");
        Debug.Print(node.Name);
        if (node.Name.ToString().StartsWith("Player"))
        {
            var parent = GetParent<Npc>();
            var bubble = parent.GetNode<BoxContainer>("OnEnterBubble");
            bubble.Visible = false;
        }
    }
}

And my log output after running a test (notice that the Debug.Print is missing):

1
Layer0
1
Layer0
1
Layer0
1
Layer0
1
Layer0
2
Layer0
Player
2
Layer0
Player
2
Layer0
Player
2
Layer0
Player
2
Layer0
Player
2
Layer0
Player
发布评论

评论列表(0)

  1. 暂无评论