Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
4 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Gallareton · May 23 at 01:52 PM · tilemapboxcollider2d

Tilemap Coliider 2D doesn't trigger on Y axis

Hello, I'm just starting to learn Unity.
I'm watching this tutorial on making a 2D game, it's a little old and some things changed in the UI but I made my best to do every step up to 1:29:55, where my problem appears.
I have a player_0 with BoxCollider2D, TEST_NPC with BoxCollider2D and a Tilemap with TilemapCollider. The Tilemap is on Layer named "Blocking", player and NPC are on Layer named "Actor".
Following script is what I'm using for move and simple collision detection, but as player collides with NPC properly, it collides with walls on "Blocking" Layer only on X axis. Y axis doesn't trigger a hit at all.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Player : MonoBehaviour
 {
     public float movementSpeed = 2.0f;
 
     private BoxCollider2D boxCollider;
     private Vector3 moveDelta;
     private RaycastHit2D hit;
     
     private void Start()
     {
         boxCollider = GetComponent<BoxCollider2D>();
     }
     
     private void FixedUpdate()
     {
         moveDelta = Vector3.zero;
         
         float x = Input.GetAxisRaw("Horizontal");
         float y = Input.GetAxisRaw("Vertical");
         
         moveDelta = new Vector3(x, y, 0);
         
         if (moveDelta.x > 0) {
             transform.localScale = Vector3.one;
         } else if (moveDelta.x < 0) {
             transform.localScale = new Vector3(-1, 1, 0);
         }
 
         Vector3 boxPosition = new(transform.position.x + boxCollider.offset.x, transform.position.y + boxCollider.offset.y, 0);
         Debug.Log(boxPosition);
 
         hit = Physics2D.BoxCast(
             boxPosition,
             boxCollider.size,
             0,
             new Vector2(moveDelta.x, 0),
             Mathf.Abs(moveDelta.x * Time.deltaTime),
             LayerMask.GetMask("Actor", "Blocking")
         );
 
         if (hit.collider == null)
         {
             transform.Translate(movementSpeed * (moveDelta.x * Time.deltaTime), 0, 0);
         }
 
         hit = Physics2D.BoxCast(
             boxPosition, 
             boxCollider.size, 
             0, 
             new Vector2(0, moveDelta.y), 
             Mathf.Abs(moveDelta.y * Time.deltaTime), 
             LayerMask.GetMask("Actor", "Blocking")
         );
         
         if (hit.collider == null)
         {
             transform.Translate(0, movementSpeed * (moveDelta.y * Time.deltaTime), 0);
         }
 
     }
 }
 

The Collision Tilemap in my example is just these 2 squares for test purposes, as you see I use really default values and X and Y axes should work the same. tilemap Player looks like this (NPC is the same, without Player script): player Please advice, I'm a little confused about why would the Y axis work differently to X axis.

collision.jpg (299.4 kB)
player.jpg (339.4 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Gallareton · May 23 at 01:21 PM 0
Share

I found out what's wrong. I updated the moveDelta by movementSpeed paramenter after asserting the hitbox, so It wen't right through them on axis that is shorter.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

188 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Player can only push one crate at a time 0 Answers

Player get's stuck on Tilemap Collider 2D 0 Answers

[HELP] Player passes through red tiles even though it shouldn't 1 Answer

Why is my BoxCollider2D with IsTrigger checked physics-colliding with other colliders without isTrigger checked? 0 Answers

How To Smoothly Climb Over Tiles? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges