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 Petya · Mar 25, 2016 at 05:51 PM · 2d-platformercollider2d

Procedural level generation with colliders

I'm about to start working on procedural level generation for a 2D platformer. First I'm creating the tile prefabs for the generator to use. So I have multiple tiles in the scene, each with their own box collider.

The problem is, when my character (uses its own box collider) walks over the tiles, it's kinda glitchy. His feet will glitch out (because the character thinks he's in the air), and sometime just stops and isn't able to move.

Here's a gif demonstrating: http://i.imgur.com/qdRD3J6.gifv

Any advice on what to do?

I don't want to have to write an algorithm at runtime that'll find all continuous chains of tiles and create a box collider that'll wrap that whole thing. Though I can, it's just a fair bit of work.

Comment
Add comment · Show 4
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 TobiasW · Mar 25, 2016 at 06:19 PM 0
Share

What do you use to find out whether the character is in the air/can move?

avatar image Petya TobiasW · Mar 25, 2016 at 06:23 PM 0
Share

The character is considered in the air if the character's collider is not touching a collider with the 'Solid' tag, which all the tiles in the level have.

And I don't do anything to figure out if the character can move. That's just up to the collider.

avatar image lassade Petya · Mar 25, 2016 at 06:40 PM 0
Share

Are u using OnColliderExit ? because between tiles it will be get called after OnColliderEnter and OnColliderStay so the player will think its in air for just a frame , when the OnColliderStay kics in again to tell its not.

$$anonymous$$akes sense?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by lassade · Mar 25, 2016 at 08:05 PM

Look at this: http://docs.unity3d.com/Manual/ExecutionOrder.html

The FixedUpdate is called before any OnCollider event so on FixedUpdate set playerIsInAir = true; and in your OnColliderStay set playerIsInAir = false; if the collider tag is the one you are looking for. This sould give you the rigth value at the time the Update function gets called.

 bool playerIsInAir;

 void FixedUpdate()
 {
     // player is in the air
     playerIsInAir = true;
 }
 
 void OnCollisionStay(Collision collision) {
     if (collision.collider.tag == "Solid") {
         // I'm touching the floor or the wall
         playerIsInAir = false;
     }
 }

Using OnCollider to tell if the player is on the ground is not a very good solution because if the player is touching a wall but not a the ground your script will think its on solid ground.

Try instead using the Unity 2D Plataformer example (https://www.assetstore.unity3d.com/en/#!/content/11228) they do a raycast to the ground that go far as about 1 pixel bellow the player an if thay ray cast finds a collider with "Solid" tag if will tell player is in the ground.

Its some like this:

 void Update ()
 {
     RaycastHit hitInfo;
     playerIsInAir = true;
     if (Physics.Raycast(transform.position, Vector3.down, out hitInfo, playerHalfHeigth + 1)) {
         if (hitInfo.collider.tag == "Solid") {
             // I'm touching the floor (dont care about walls)
             playerIsInAir = false;
         }
     }
 }

It works with Physics2D as well.

Comment
Add comment · Show 6 · 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
avatar image Petya · Mar 25, 2016 at 08:11 PM 0
Share

I'm using the bounds of the collision to deter$$anonymous$$e if my player is colliding with the ground or a wall, so I'm not too worried about that. I'll look into setting the playerInAir state in the FixedUpdate() first to see how that pans out. If not too well, perhaps I'll try it with raycasts ins$$anonymous$$d.

I'll get back to you once I give it a try, thanks!

avatar image Petya · Mar 26, 2016 at 12:23 AM 0
Share

I went with the raycast, and that fixed the character thinking he's in the air, so the legs stopped glitching, but the character still occasionally gets stuck when walking: http://i.imgur.com/b$$anonymous$$OsYJq.gif

avatar image lassade Petya · Mar 26, 2016 at 07:04 PM 0
Share

Some time ago i was reading a post about this problem (i can remember here) this may be caused if you are not using a capsule collider in your character and the tiles collider don't match perfectly (to the 9th decimal point kind of precision).

avatar image lassade Petya · Mar 26, 2016 at 07:05 PM 0
Share

how lucky here it is http://www.iforce2d.net/b2dtut/ghost-vertices

avatar image Petya lassade · Mar 28, 2016 at 02:23 PM 0
Share

Awesome, thanks I'll try that. Else I suppose I could have both a box collider for the top part of my character, and a circle collider for the bottom.

avatar image murkantilism · Mar 28, 2016 at 02:39 PM 0
Share

Raycasting seems like overkill, and can be pretty expensive. Why not simply use a Character Controller for the player? Then all you have to do is check if (playerController.isGrounded)

Alternatively you could emulate how .isGrounded works under the hood with collision flags.

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

6 People are following this question.

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

Related Questions

Large 2D polygon mesh levels 1 Answer

Moving BoxCollider2d with animation is not the right way? 2 Answers

Constant velocity when colliding - 2D 0 Answers

Make a gameobject Trigger Trigger Colliders but not Other Colliders 1 Answer

BoxCollider2D have an offset on collision,Collider2D has an collision offset? 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