Search code examples
c#monocollisiongodotcollider

Godot How to get Colliders Parent Name (C# godot mono v3.1.2 stable)


I have this code :

    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*/
    }

Here is my tree hierarchy:

Hyerarchy

Red ones are objects which are colliding. Green one is which one i want to get its name in string format.


Solution

  • I found solution myself this one works :

        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
        }