【问题标题】:Godot How to get Colliders Parent Name (C# godot mono v3.1.2 stable)Godot 如何获取 Colliders Parent Name (C# godot mono v3.1.2 stable)
【发布时间】:2020-04-20 00:24:41
【问题描述】:

我有这个代码:

    var Collide = MoveAndCollide(new Vector2(speed.y * (float)Math.Cos(teta), speed.y * (float)Math.Sin(teta)));
    if (Collide != null)
    {
        /* I need to get colliding objects parent node name here*/
    }

这是我的树层次结构:

红色的是正在碰撞的物体。绿色的一个是我想要以字符串格式命名的那个。

【问题讨论】:

    标签: c# mono collision godot collider


    【解决方案1】:

    首先查看KinematicBody.move_and_collide 的文档:

    返回一个 KinematicCollision,其中包含有关碰撞的信息。

    KinematicCollision 的文档列出了许多字段,其中之一是 collider

    碰撞体。

    请注意,主体是拥有碰撞形状的PhysicsBody不是碰撞形状本身(后者存储在collider_shape 中)。这意味着collider 将是您示例中的StaticBody,所以我们可以这样做:

        var Collide = MoveAndCollide(new Vector2(speed.y * (float)Math.Cos(teta), speed.y * (float)Math.Sin(teta)));
        if (Collide != null)
        {
            print(Collide.Collider.Name)
        }
    

    【讨论】:

    • 我不能使用 Collide.Collider.Name 错误:Object' 不包含 'Name' 的定义,并且找不到接受“Object”类型的第一个参数的扩展方法 'Name'(是您缺少 using 指令或程序集引用?)
    【解决方案2】:

    我自己找到了这个可行的解决方案:

        var Collide = MoveAndCollide(new Vector2(speed.y * (float)Math.Cos(teta), speed.y * (float)Math.Sin(teta)));
        //Just defined a MoveAndCollide
    
        if (Collide != null)
        {
            var x = (Godot.Node2D)Collide.Collider;
    
            //Use collider as a Godot Node or Node2D
            //then you can access Node properties and method like GetName()
    
            GD.PrintS(x.GetName()); // Returned speedBoost for me
        }
    

    【讨论】:

      猜你喜欢
      • 2023-01-25
      • 2018-07-04
      • 1970-01-01
      • 2022-11-19
      • 1970-01-01
      • 2021-10-27
      • 2021-04-30
      • 2021-12-26
      • 2019-01-19
      相关资源
      最近更新 更多