Attached scripts and raycasting

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Naril

Hi!

I’m new to Godot and I’ve run into an interaction I do not understand between raycasting a Kinematic2D node with an attached script on that node and that same kinematic2D node without the attached script.

Expected result - Raycasting a kinematic2D node returns a collider of that node.

Problem: When raycasting a kinematic2d node with an unrelated attached script, the resulting collider returns a reference to the attached script and NOT the root kinematic2d node.

Code I’m using to raycast with. It is attached to an otherwise empty Node2D node in my main scene.

public class TablePhysics : Node2D
{
    public override void _PhysicsProcess(float delta)
    {
        DragCheck();
    }

    void DragCheck(){ 
     
         if(Input.IsActionJustPressed("click")){
           Physics2DDirectSpaceState spaceState = GetWorld2d().DirectSpaceState;
           Vector2 position = GetGlobalMousePosition();
           Godot.Collections.Dictionary result = spaceState.IntersectRay(position, position);
           GD.Print(result["collider"].GetType());
    }

Code - unrelated Script attached to the Kinematic2D node I’m hitting with a raycast. Its only purpose is to be a data container.

public class CardScript : Node
{
    [Export] bool draggable = true;
}

the above GD.print Results of raycasting the kinematic2D body without Cardscript attached. This is the expected behavior.

Godot.KinematicBody2D

the above GD.print results of raycasting the same kinematic2D body with CardScript attached to it. I do not understand why this should be different than raycasting this node without the attached script.

CardScript

Can someone explain this interaction? Am I doing something wrong here?

:bust_in_silhouette: Reply From: Guliver_Jham

Well, when you attach a script to your kinematicbody, it kinda becomes it, it’s normal.

Interesting. Thanks for the answer. I was scratching my head for quite a while on this.

Naril | 2023-02-06 18:38