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 Yundahan · Mar 18, 2020 at 07:44 PM · collider2dplatformerplatformercontroller

Collider2D information outdated?,

Hey, I am quite new to Unity and I just started working on a 2D platformer, more precisely on the movement of the playable character. Now, when it comes to collision detection, the information returned to me by the Collider2D seems to be outdated, as I can enter one frame's worth of movement into any wall and will only get pushed back to where I am supposed to be once I let go of the button. So if my character is walking right and should be at x = 15 to just touch the wall, he will be stuck at x = 15.1 until I let go of the right arrow key. Maybe I just messed up the order of code or maybe something else, whatever it is, if anyone could help me, I would be most grateful. The code is given below.

 void FixedUpdate()
 {
     move = Input.GetAxisRaw("Horizontal");
     velocity.x = Mathf.MoveTowards(velocity.x, speed * move, Mathf.Abs(move) * acceleration + (1 - Mathf.Abs(move)) * deceleration);
     transform.Translate(velocity);
     Collider2D[] hits = Physics2D.OverlapBoxAll(transform.position, boxCollider.size, 0);
     
     Debug.Log(transform.position.x);
     
     foreach(Collider2D hit in hits)//push the character out of potential intersections after movement
     {
         if(!hit.isTrigger)
         {
             ColliderDistance2D colliderDistance = hit.Distance(boxCollider);

             if (colliderDistance.isOverlapped && colliderDistance.isValid)
             {
                 transform.Translate(colliderDistance.normal * Mathf.Abs(colliderDistance.distance));
                 Debug.Log(Mathf.Abs(colliderDistance.distance));
             }
         }
     }
 },
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 Bunny83 · Mar 19, 2020 at 02:57 AM

You don't move with the physics system at all. You manually force move the object using the transform component and then let the physics system detect the overlap. This is not how the physics system should be used. You essentially try to manually implement your own physics system on top of Unity's Box2d physics by just picking out certain information from the physics system.


Note that Box2d (Unity's "Physics2D" engine) as well Nvidia's PhysX (Unity's 3d "Physics" engine) have their own representations of all objects relevant for physical interactions. Unity does not automatically update those whenever you change the corresponding object in the Unity engine. This is done automatically when Unity performs a physics step. However since you essentially roll your own system the syncronisation of all colliders happens the next frame. You could use Physics2D.SyncTransforms after you changed the position / rotation of a collider and before you try to check any overlaps using the Physics2d system.


Over all I don't really get the point of rolling your own rudimentary collision response implementation. You don't modify the velocity when a collision happens. Is there a reason you don't want to use Unity's rigidbody simulation?


ps: Note that in general, moving a collider without a rigidbody can cause huge performance issues because all colliders without a rigidbody are considered stationary. They might be baked into one static collision model. So moving individual colliders would essentially rip that model apart each time a collider is moved.

Comment
Add comment · Show 1 · 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 Yundahan · Mar 19, 2020 at 09:20 AM 0
Share

First of all thank you very much for your informative response :)

The reason I didn't use RigidBodies was that I was following a rough guideline on how to do a 2D character controller that i found on the internet, however, after reading your insights on the matter, I feel like I didn't find the best tutorial out there.

The last point you address in your PS sound like quite a big deal, should I avoid moving individual colliders at all costs or is it not as bad as it might sound to "rip the model apart"?

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

130 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

Related Questions

How to check from which side a gameobject collide? 0 Answers

CharecterController.Move() ignores parents movement 0 Answers

Dropping down from 2D platforms: Character gets "sprung back" if collision resumes mid-collider 1 Answer

2D 360 degress platformer example needed 0 Answers

Messed up character controls on Android Platform 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