Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
774 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 Reply

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().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...