Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by jacobw56 · Dec 08, 2018 at 06:39 PM · colliderrigidbody2dcollider2dtilemap

Tilemap collider - character passes through wall

I have a character that moves tile-by-tile with the following simple controller:

 public class PlayerController : MonoBehaviour {

     private Vector2 speedX = new Vector2(1, 0);
     private Vector2 speedY = new Vector2(0, 1);
     private Vector2 startPosition;

     private Rigidbody2D rb;

     // Use this for initialization
     void Start ()
     {
         rb = GetComponent<Rigidbody2D>();
     }
 
     // Update is called once per frame
     void Update ()
     {
         startPosition = transform.position;
         if (Input.GetKeyDown("left"))
         {
             rb.position = (startPosition - speedX);
         }
         else if (Input.GetKeyDown("right"))
         {
             rb.position = (startPosition + speedX);
         }
         else if (Input.GetKeyDown("up"))
         {
             rb.position = (startPosition + speedY);
         }
         else if (Input.GetKeyDown("down"))
         {
             rb.position = (startPosition - speedY);
         }
     }
 }


Player has Rigidbody 2D (Dynamic) and Circle Collider (isTrigger is off and same behavior with Box Collider). The wall Tilemap layer has Tilemap Collider 2D, Rigidbody 2D (Static), and Composite Collider 2D (again, isTrigger is off on the colliders).

The problem is that the player bounces off the wall a few times but then slips through the wall eventually. Even worse, sometimes when it passes through, it is offset by a fraction of a tile (usually 0.5) and can then pass freely through everything!

My guess is that the tile offset occurs because the "bounce" off the wall is not restricted to exclusively X or Y movement (no idea how to implement that), but I have no clue why the passing-through occurs. I tried fiddling with the Game's Time setting thinking it was because I was hitting the button too fast, but it makes no difference and the player passes through walls regardless of the speed that I repeat the movement.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by grodriguezbriseno · Mar 12, 2020 at 07:38 AM

For a Dynamic Rigidbody, you should use velocity instead of position to move the character around.

Comment
Add comment · Share
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

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

126 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

Related Questions

How do I get collisions between Tilemap Collider 2d and a Kinematic Rigidbody 2d? 1 Answer

Player Gets Stuck On TilemapCollider2D 0 Answers

Sprite Gets Stuck With Frozen Rotation 0 Answers

Tilemap Collider 2D 2 Answers

HOW TO CALLBACK COLLIDER WITH NAME? 2 Answers


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