I'm making a platformer where there are certain tiles that interact with the player's movement.
Right now, I need to be able to detect collisions with specific layers, since I feel that is the neatest way to do it.
I assigned each layer as a different type of block, and I want an event to trigger when a player lands on one of them.
Thanks in advance.
How to detect collisions with specific layers
- Edited
You'll have to use these settings which are found in your kinematic/rigid/static etc body settings
The layer matrix indicates in what layer the selected body is, while the mask matrix indicates with what other layers the body will detect collisions with
So you just need to have your ground be a layer and then select that layer in the payer's mask matrix
Also do the same in the grounds properties i.e. select the player's layer in the grounds mask property
https://docs.godotengine.org/en/3.2/tutorials/physics/physics_introduction.html#collision-layers-and-masks
This link will be helpful for you
How do I detect what layer a body collides with with code?
Also, the doc you used is from 3.2, currently I am using 4.2.
Vickyboi In the lower left of the documentation page you can click on the version number to change it to the corresponding page for your version. To check collision layers in code you would use the collision_layer
and/or collision_mask
properties as explained on that page.
- Edited
soundgnome
What lines of code would I need?
I read all of the doc, yet it only mentioned the properties, not how to utilize them.
I tried tweaking the code provided in the doc, but I couldn't formulate a way to detect a collision layer.
func _physics_process(delta):
var collision_info = move_and_collide(velocity * delta)
if collision_info:
var collision_point = collision_info.position
How would I modify this code to make it detect when I collide with a specific layer.
Is there anything else I should be trying?
- Edited
Vickyboi You can call get_collider
on collision_info
and then check the collision_layer
of the returned object. Something like:
var collision_info = move_and_collide(velocity * delta)
if collision_info:
var collider = collision_info.get_collider()
collider.collision_layer # etc
soundgnome
It may be important to mention that I am trying to collide with tilemaps
I tried to print the print(collider.collision_layer), but it returned: Invalid get index 'collision_layer' (on base: 'TileMap').
soundgnome
Thanks! This video has revealed some very important details to solve the problem.
However, I cannot find how to grab the layer number in a 2d platformer setting, even after some heavy research.
I may have to utilize a different approach for interacting with tilemaps, any suggestions?
Vickyboi So in that video he used custom data layers (rather than collision layers) to flag tiles to trigger specific interactions, he shows how to set it up starting at around 2:10.
@"soundgnome"#p11092
Would this approach work in a 2d side scrolling platformer?
I think I can do it now (hopefully). Just to clarify, I wouldn't need ALL of the code in the terrain detector, just the ones he shows?
soundgnome
I finally did it! Thank you for sticking with me through this process, I'm sorry for how annoying it might've been. I can finally continue my game!
Vickyboi Glad you got it worked out! And no need to apologize, I just didn't see your previous reply (looks like something went weird with the "@" and I didn't get a notification).
That video is great, but I still need to get the collision layer, not custom data, so the vid doesn't really solve the problem
Vickyboi can you please share how you did it?