Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
764 views
in Technique[技术] by (71.8m points)

2d - Godot raycast2D area detection not working

I'm relatively new to Godot and GDScript, but am learning rapidly so I've started trying to make a 2D platformer where you can move around, and climb and slide on walls, jump from walls and stay on walls. I'm using raycast2D going from the player on the x axis to detect when the player is touching an area2D (in this case a tile that is also a staticbody2D). To test this, I wrote:

onready var raycast = get_node("RayCast2D")

func _physics_process(delta):

if raycast.collide_with_areas == true:
     print("area")

The problem I'm having is the console is printing area all the time, which means that all of my tiles are area2Ds (some of them are just staticbody2D). Why are all my tiles area2Ds ?

question from:https://stackoverflow.com/questions/65854268/godot-raycast2d-area-detection-not-working

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

From the RayCast2D class documentation:

bool collide_with_areas [default: false]
     set_collide_with_areas(value) setter
     is_collide_with_areas_enabled() getter

If true, collision with Area2Ds will be reported.

You're using collide_with_areas as though it indicates whether the raycast is currently colliding with areas, while in truth it is a class property denoting whether the raycast should report collisions with areas at all.

Instead, you should be setting collide_with_areas (and enabled!) to true (either through the inspector, or via code) and then calling raycast.get_collider().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...