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 /
  • Help Room /
avatar image
0
Question by AAcat · Aug 05, 2019 at 12:42 AM · 2d gamerigidbody2dcollider2dglitchgrid based game

2D Colliders don't actually touch, causing problems

I tried asking this question before, but I guess the question never got posted for some reason. I have this script I made so the character moves on a grid-based system. To make the transition between the grid space seemless, each grid is 8x8 pxiels long/wide, and so each tile (still 8x8) on the tilemap is going to be 1 unit. 1/8 = 0.125 so the character moves 0.125 per update until they reach a whole number. Here is the code for it:

     private Rigidbody2D con;
     public bool up;
     public bool down;
     public bool left;
     public bool right;
 
     public bool inputer;
 
     private Vector2 rict;
     public bool upc;
     public bool downc;
     public bool leftc;
     public bool rightc;
     
     void Start () {
         con = GetComponent<Rigidbody2D> ();
         con.MovePosition(new Vector2(Mathf.Round(con.position.x), Mathf.Round(con.position.y)));
     }
     
     void FixedUpdate () {
         if(right || left || up || down) {inputer = false;}else{inputer = true;}
         if(inputer && Input.GetAxisRaw ("Horizontal") > 0 && !rightc){leftc = false;downc= false;upc= false; right = true;rict = new Vector2 (0.125f,  0);con.MovePosition(con.position + rict);}//rict = new Vector2 (0.25f,  0);
         else if(inputer && Input.GetAxisRaw ("Horizontal") < 0 && !leftc){rightc = false;downc= false;upc= false; left = true;rict = new Vector2 (-0.125f,  0);con.MovePosition(con.position + rict);}//rict = new Vector2 (-0.25f,  0);
         else if(inputer && Input.GetAxisRaw ("Vertical") > 0 && !upc){downc= false;leftc = false;rightc = false; up = true;rict = new Vector2 (0,  0.125f);con.MovePosition(con.position + rict);}//rict = new Vector2 (0,  0.25f);
         else if(inputer && Input.GetAxisRaw ("Vertical") < 0 && !downc){upc= false;leftc = false;rightc = false; down = true;rict = new Vector2 (0,  -0.125f);con.MovePosition(con.position + rict);}//rict = new Vector2 (0,  -0.25f);
         else if (right && con.position.x < Mathf.Ceil(con.position.x)){con.MovePosition(con.position + rict);}
         else if (left && con.position.x > Mathf.Floor(con.position.x)){con.MovePosition(con.position + rict);}
         else if (up && con.position.y < Mathf.Ceil(con.position.y)){con.MovePosition(con.position + rict);}
         else if (down && con.position.y > Mathf.Floor(con.position.y)){con.MovePosition(con.position + rict);}
         else if (right) {right = false;}
         else if (left) {left = false;}
         else if (up) {up = false;}
         else if (down) {down = false;}
         else {rict = new Vector2();}
 
         if(con.position.x % 0.125 != 0){con.MovePosition(new Vector2(Mathf.Round(con.position.x), con.position.y)); if (right) {right = false; rightc = true;} else if (left) {left = false; leftc = true;} else if (up) {up = false; upc = true;} else if (down) {down = false; downc = true;}}
         if(con.position.y % 0.125 != 0){con.MovePosition(new Vector2(con.position.x, Mathf.Round(con.position.y)));if (right) {right = false; rightc = true;} else if (left) {left = false; leftc = true;} else if (up) {up = false; upc = true;} else if (down) {down = false; downc = true;}}
     }
 }

As you can see, the if statement at the end is for collision detection, if the character hits a wall or some kind of collider, it will round back to the previous position. This is my temporary solution to another problem I was having, and I don't like how this one works. I had the character's box collider pixel perfect, it was the right size and everything, because of that if the player hit a wall, it wouldn't actually stop the player but softlock the player. Their movement wouldn't register because, in Unity, collision detection doesn't actually mean touching if the player gets too close to a wall (a fraction of a fraction) they player will actually not touch the collider, this seems to be just how the engine works. so if the player is not exactly moving properly, and the collider moves the character a fraction of a fraction, then that character will never truly be a whole number as a result. To circumvent this I made the player's collision box slightly smaller so that when they are standing next to a wall they don't get pushed off the grid layout by a slight (and unnoticeable) amount which would softlock the player. So what's the problem I'm still having? Because the player's 2d collider box is smaller than they are supposed to if they hit a wall they will be jolted back to the previous grid tile. This jolt animation is something I don't want. I have YT videos showing what I mean, but I don't think I can post them here, because last time I posted a question with a video of what I needed help with, the post never got posted due to a link. So hopefully this worded description is enough to get you understanding. If you need more description ill gladly try my best.

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

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

206 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 avatar image avatar image avatar image avatar image avatar image avatar image 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

All colliders not longer working 0 Answers

How to prevent a 2D Kinematic asset passing through a 2D Box Collider. 1 Answer

No overload for method. (Question) 0 Answers

Do I need Rigid2DBody if I don't plan to use physics? 2 Answers

Can someone explain how this collision detection work and why it fails sometimes? 0 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