Topic was automatically imported from the old Question2Answer platform.
Asked By
Dray98
I was able to apply one way collision for tileset by:
Tileset → physics layer → polygon 0 → one way
But im struggling with changing the direction of the polygon face, so that i can change the direction of the one way collision. I’ve tried to rotate it but it is still the same
For CollisionPolygon2D, if i rotate it, the direction follows the rotation of the face, but this is not the same in tileset polygon.
Does anyone know how to do it?
Here’s a test code for auto instantiate collision2D:
extends Node2D
@export var tilemap: TileMap
@export var up_wall_scene: PackedScene
@export var down_wall_scene: PackedScene
@export var left_wall_scene: PackedScene
@export var right_wall_scene: PackedScene
var layer = 0
func create_wall(wall_scene,pos):
var wall = wall_scene.instantiate()
wall.position = to_global(tilemap.map_to_local(pos))
add_child(wall)
func _ready():
var cell_arr = tilemap.get_used_cells(layer)
for cell_pos in cell_arr:
var data = tilemap.get_cell_tile_data(layer, cell_pos)
if data:
var ID = data.get_custom_data_by_layer_id(layer)
match ID:
2:
create_wall(up_wall_scene,cell_pos)
4:
create_wall(left_wall_scene,cell_pos)
5:
create_wall(right_wall_scene,cell_pos)
7:
create_wall(down_wall_scene,cell_pos)
You have to add custom data for the tiles you want, mine was 0-9 and 2,4,5, and 7 is the wall tile that I’m interested in. Also, if you saw that the Collision did not spawn at the correct coordinate, its probably due to the offset of that scene, so remember to open that scene and try to center or move around to get what you need. For my wall_scenes, i used StaticBody2D and CollisionShape2D as a child
I think your question is how to rotate an individual tile in a tilemap. I did some search and found this individual tile manipulation.
I don’t think it’s in the Godot yet.
For the moment, I think you can try to make the platform separate from the tilemap. Just change the platform to the static body and use code to place it. I think Tilemap support placing node as well, so you can also try that. If you want to do this purely with Tilemap, I think you need to add a script that monitors the rotation and contact point with the player, and many more to make the oneway collision.
Thanks for the reply!!!
I just wanted to rotate the collision shape, not the tile, but I think rotate the tile can be a workaround as well.
Example, there’s a wall that allows player to enter but not exit. Currently the one way polygon in tileset allows player to enter from below the wall only. If I rotate the wall tile so it only allows player to enter from above wall tile, from a top down art, the wall will look weird.
Currently, i created a scene with alot of collision2d inside, and manually change each of them to behave. This method is too tedious, I think I will research a way to place using code like you said.
Thankssss
Dray98 | 2023-06-09 04:26
hey did you ever find a way to use code or a different way for this? I have been trying to figure it out for a while now and I just hit a wall
Challedy | 2023-06-19 13:44
I thought of a new way where it uses Area2D to change character collision mask.
All common obstacle tile will have collision on mask layer 1 and 2.
Special tile that I want it to have one way collision will have mask layer 1 only.
When player enters the Area2D, it will change player’s collision layer to 2 only, else will be 1.
This works for me now because I don’t have a lot of place that need them.
If you want to automate it using code, you can try this:
Instantiate the collision2d scenes at the position of the specific tile.
This is just a rough idea, I haven’t really tested it out.
Dray98 | 2023-06-20 10:37
Damn that’s kinda smart…
I ended on smt similar
I changed the collision shape of the player whenever I pressed “W” or “S” but that messed with the collisionshape for combat.
Thank you so much for finding a much smoother way to do this ^^
Here’s a test code for auto instantiate collisionShape2D based on tile custom data:
extends Node2D
@export var tilemap: TileMap
@export var up_wall_scene: PackedScene
@export var down_wall_scene: PackedScene
@export var left_wall_scene: PackedScene
@export var right_wall_scene: PackedScene
var layer = 0
func create_wall(wall_scene,pos):
var wall = wall_scene.instantiate()
wall.position = to_global(tilemap.map_to_local(pos))
add_child(wall)
func _ready():
var cell_arr = tilemap.get_used_cells(layer)
for cell_pos in cell_arr:
var data = tilemap.get_cell_tile_data(layer, cell_pos)
if data:
var ID = data.get_custom_data_by_layer_id(layer)
match ID:
2:
create_wall(up_wall_scene,cell_pos)
4:
create_wall(left_wall_scene,cell_pos)
5:
create_wall(right_wall_scene,cell_pos)
7:
create_wall(down_wall_scene,cell_pos)
You have to add custom data for the tiles you want, mine was 0-9 and 2,4,5, and 7 is the wall tile that I’m interested in. Also, if you saw that the Collision did not spawn at the correct coordinate, its probably due to the offset of that scene, so remember to open that scene and try to center or move around to get what you need. For my wall_scenes, i used StaticBody2D and CollisionShape2D as a child