Raycast cast to AggroArea/CollisisionShape Radius


I found myself wondering, is it possible to set the RayCast2D node Cast To property via code. I saw that the property can be set as a Vector2D and was excited! So I thought, why not set the Cast To property to be based on the radius size of the AggroArea/CollisionShape2D? I'm struggling to find how to access the radius property of the CollisionShape2D object. Is this possible? I figured it would make sense to set the Cast To property based on the size of the radius such that, in the future if I changed the size of the detection Radius, the RayCast Cast To property would dynamically update. As the code sits in the course, if you changed the size of the AggroArea/Collisionshape2D Radius, you'd also have to go back and set the RayCast2D Cast To property.

  • R
    RomanGnome replied

    Of course after a bit more poking around with objects and I found it to be possible haha.

    Here is an example of the changes I made that enables you to rely on the CollisionShape2D Radius value for the RayCast2D Cast To property Value.

    onready var aggro_collision: CollisionShape2D = $AggroArea/CollisionShape2D

    func _physics_process(delta: float) -> void:

     var direction := Vector2.UP

     if target:

      raycast.cast_to = Vector2(aggro_collision.shape.get("radius"), 0)


    I am curious though, is there a better way to do this? Should this be avoided? Are there benefits to setting these values statically (on the radius itself and on the raycast object itself)?