

If you use CollisionShape( 2D/ 3D), make sure to set the shape to your CollisionShape( 2D/ 3D). Adding a sprite or other graphic node is only concerned with graphics and has no effect in physics. This is what defines their size and shape as far as physics is concerned. The kinematic/character, static, and rigid bodies, and also area, need CollisionShape( 2D/ 3D) or CollisionPolygon( 2D/ 3D) as child. If you want to set the collision_layer and collision_mask properties from code, you need to keep in mind that they are sets of binary flags. Regardless, try to be efficient with your layers. And for some people that is not enough, and we will have to resource to other ways of filtering we will see later. You have a total of 32 layers to work with. That is, collisions are checked both ways. ※: Actually an object will collide with whatever they specify in their collision mask, and any object that specify them in their collision mask. And everything collides with ground and walls. Similarly, the enemy characters probably won't interact with collectibles. The player character probably won't interact with player projectiles, and enemy character won't interact with enemy projectiles. For example, an enemy projectile should not collide with other enemy projectiles (and telling Godot to not check for those collision can help with performance). Sometimes it is easier to think about what they can't collide with. In the collision_mask you set what they can collide with※. Then you give each physics object its own collision_layer depending on what they are. You can go to Project Settings -> General -> Layer names -> 2d Physics (or 3d Physics if you are working in 3D), and give names to the layers there.įor example, you may have layers for these (depending on the kind o game you are doing): Otherwise, it can be useful to define some layers. To begin with, the default values of the collision_layer and collision_mask properties is set so that everything collides with everything.
#GODOT RAYCAST CODE#
I'll mention any case when you need to write code somewhere else.

I'll specify when something is specific of Godot 3 or Godot 4.Īlso, since we are talking about physics, in general you will be working in _physics_process(…). You will also find that I say "kinematic/character" that is referring to a kinematic body in Godot 3, or a character body in Godot 4. If I say Class( 2D/ 3D), I mean that what I'm saying applies for both Class and Class2D. On that note, just in case, know that 3D objects can't collide with 2D objects. While the question is about 2D, for 3D it is mostly the same provided you use the 3D versions of the nodes. No, you don't need to know all that, but you asked for "everything very thoughrouly". And I'll mention some stuff about raycast and area, and other queries at the end. Then filtering physic bodies and communication.
#GODOT RAYCAST HOW TO#
Where & how to connect and write the codeįor this answer I will start by explaining collision layers and mask.

Where and how do I add a collision shape 2d or area2d or other nodes along those lines. some examples of things I would like to be more clear are Please explain everything very thoughrouly as I have been trying for the past couple of days to figure it out but most of the places I have looked I didn't understand what to do and when I copied the code it didn't work. I want to detect when the mob touches the player and give an output finally I have the main scene with has the player scene inside it and instances the mob scene every so often to spawn mobs at the top of the screen. this scene only has the sprite and a tiny piece of code that makes it so that the mob deletes itself once it leaves the screen using the visibility notifier(I also turned of the mask square so there would be no physic). I also have a scene named "mob.tscn" which is a rigidbody2d node. this scene is a player which moves from left to right across the screen. one that is named "KinematicBody2D.tscn" which a KinematicBody2D node.
