0
\$\begingroup\$

I've just started out with my first game in Unity. It's a simple 2D Platformer with a tile-based level.

enter image description here

When coding in the movement, I used the Physics2D.OverlapCircle() and an empty to check whether my player was touching the ground. However, I need to use a layer to make sure only the ground is detected.

my code below

//fixed update used for physics calculations
void FixedUpdate()
{
    //right or left arrows being pressed
    float moveX = Input.GetAxis("Horizontal");

    Vector2 moveDir = new Vector2(moveX * playerSpeed, rb.velocity.y);
    rb.velocity = moveDir;
}
 
//update used for spacebar input and force
void Update()
{
    if (Input.GetKeyDown(KeyCode.Space)) 
    {
        rb.velocity = Vector2.up * jumpForce*Time.deltaTime;
    }
    
    //is my circle overlapping any other colliders
    Collider2D collider =  Physics2D.OverlapCircle(feet.position, checkRadius, groundLayer);
    Debug.Log(collider); 
}

When I tried assigning layers to my tiles, I couldn't find a way to do it.

I would be really grateful if anyone could show me how to, or if anyone can tell me a more efficient way to deal with jumping in a 2D platformer.

\$\endgroup\$
4
  • \$\begingroup\$ Have you considered placing a second tilemap under the same grid for your non-walkable tiles? \$\endgroup\$
    – DMGregory
    Commented Sep 13, 2020 at 12:35
  • \$\begingroup\$ Thank you for your help! It's working wonderfully right now. \$\endgroup\$ Commented Sep 26, 2020 at 1:21
  • \$\begingroup\$ I'm glad you've solved your problem! Want to write up your solution as an Answer below to help future users? \$\endgroup\$
    – DMGregory
    Commented Sep 26, 2020 at 1:23
  • \$\begingroup\$ Btw: in the line Collider2D collider = Physics2D.OverlapCircle(... you are not checking for other colliders, you are checking only for the first collider being found. That could lead to bugs if there is more than one collider overlapping the circle. You might want to use Physics2D.OverlapCircleAll instead and then use a for-loop to check if any of the returned colliders is relevant here. \$\endgroup\$
    – Philipp
    Commented Jul 11 at 6:26

2 Answers 2

1
\$\begingroup\$

You are using a tilemap. That means tiles aren't game objects. All the tiles on a tilemap become a part of the Tilemap game object. If you just want to set whether a tile in your tile palette is solid or decorative, then you do that by setting the "Collider Type" in the settings of the tile asset. The colliders of all tiles on a Tilemap are then combined into the TilemapCollider2D component of the Tilemap game object. Which means that all the tiles of the tilemap are welded into one solid game object. So if you want to filter collisions with tiles using physics layers, you need to assign that layer to the whole Tilemap.

Unfortunately there is no way to assign tiles on the same Tilemap to different layers. If you need this functionality for some reason, then you can use multiple tilemaps with different physics layers. Or if the objects that require a different physics layers aren't too many, you might want to consider to represent them with regular game objects on top of the tilemap instead.

\$\endgroup\$
-1
\$\begingroup\$

I'm Assuming you are using tiles as prefab. So just change the layer of walkable tiles in the main prefab.

Also, you can manually add the layer via script.Referance

Make a tile script and add it to the tile prefab.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ From the screenshot, it looks like they're using a tilemap component, not individual prefabs per tile. \$\endgroup\$
    – DMGregory
    Commented Sep 14, 2020 at 12:13
  • \$\begingroup\$ Yes I am using the tilemap system. Thank you for your help anyway. \$\endgroup\$ Commented Sep 26, 2020 at 1:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .