Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Bamboy · Feb 09, 2015 at 08:58 PM · physicsrigidbody2dphysics2dcollider2dcollision2d

Workarounds for Higher Control When Using Physics2D?

It's been a very long while since I've been on here asking for help. I've come a very long way in terms of my skills as a programmer, and I wouldn't have made it so far without the community to help jump start me. (Or at least not as quickly.) Cheers! :D


ANYWAY, I am working on a top-down 2D physics game where the main mechanic is bumping enemies into environment hazards. Right now I am trying to just make the prototype.

I keep having this re-occurring problem where I can't quite get the right feel in terms of balancing:

  • Collisions.

  • Sliding. (After being 'bumped back'.)

  • Player (and AI) input control.

I thought I fixed it a few days ago (after many experiments) but then I introduced a few walls and then a character collided with the another character, which bumped them into the wall, bounced back, then hit the original character at even higher speeds, and they both go flying off into oblivion at insane speeds. Also occasionally, collisions will cause one or both characters to react to the collision 'wrongly', such as moving perpendicular to the original velocity. I haven't been able to pinpoint what exactly is causing this.

('Character' refers to AI or the Player)(Note that scripting aspects are shared between characters)

Here's my current setup:

  1. Rigidbody2D and CircleCollider2D on all characters. Linear Drag = 7.5, Angular drag = High number, Gravity = 0, Interpolate = Extrapolate, Collision detection = Continuous, Mass = 7.5 on the player, 2 - 4 on AI. (This is to make it easier for the player)

  2. OnCollisionEnter2D: This frame's velocity MINUS last frame's velocity. Normalize the result and feed it into the other character's 'Knockback' function.

  3. Knockback(direction, duration): Initialize a new knockback, based on direction and duration. The variables set in this will cause linear drag to be reduced. We do a delayed AddForce so that we know drag is reduced first. See #5: StartCoroutine("Push", direction) This combined with the fact that this is being applied upon top of the collision knockback is what is probably causing our massive velocity.

  4. Update(): Set rigidbody2D drag to a percentage of our original drag. An input percentage multiplier does the same to our input. The percentage of both of these values is based on the time since the last knockback happened, and our knockback duration. A zero to one value is created between these two. I call this function 'ReverseLerp'. :D The zero-one value is then fed into AnimationCurve.Evaluate() to help smooth out the recovery. Increasing drag over time is a good way to speed us up in the beginning and then slow us down near the end, but there may be a better method. PROBLEM WITH THIS: Further collisions that happen with this cause additional force to be applied to the object by the physics engine. Oh, also after all of this, we do AddForce( input inputPercentage 1.5f * mass, ForceMode2D.Force )

  5. Push(direction): WaitForFixedUpdate(), then do AddForce(direction * 2.5, Forcemode2D.Impulse) Remember this function was called earlier from Knockback().

  6. Also in FixedUpdate(), we limit our velocity if it exceeds a certain magnitude. Velocity = velocity.normalized * maxVelocity; maxVelocity is shared between characters. This was added to try to fix the huge velocity problem. It helps a bit but not enough.

This is pretty much my setup that's giving me problems.


I've researched and considered some other solutions and possible methods of giving a more reliable balance...

  • Detect before the actual collision, which object has more force, and then give it 'dominance'. When the actual collision happens, make the non-dominant object take the full force of the collision somehow, and completely or mostly stop the 'dominant' object. I don't really know how I could override the physics engine's collisions like this without using isKinematic and doing this manually for both objects, which could cause some unrealistic looking physics, especially if a third object is involved.

  • Detecting collisions before they happen could be detected using a larger collider, as a trigger. Could cause problems if an object just barely misses an actual collision, as well as if we have very fast-moving objects.

  • Just completely create my own physics simulation using isKinematic, which I am not sure I have the mathematical knowledge and skills to do, even for 2D...


Questions:

  • What is a method of getting what force (as velocity) was gained or lost from a single rigidbody involved in a collision? (JUST the collision, and not the changes in force caused by other means, such as ApplyForce, or possibly drag?) If I could find this out, I could effectively 'remove' the change, normalize it, then re-add it at whatever magnitude I wanted, giving increased control.

  • What are your ideas on how to approach this problem? Any other workarounds that might be useful? Experiences or similar problems you've had? Solutions? I'm looking for any and all possible options at this point.

I just need this all to work well; It is the core gameplay mechanic, after all.

Offtopic sidenote: I swear every time I come on here to ask something, I end up writing this over-detailed long thing even though I tell myself that I won't this time. Sorry.

Comment
Add comment · Show 2
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 Bamboy · Feb 12, 2015 at 06:23 PM 0
Share

Anyone? No?

avatar image smoggach · Feb 12, 2015 at 06:50 PM 0
Share

I didn't see you mention anywhere the physics2D materials that you use. I'd try to get the effect I wanted without writing any code at all first using the rigidbodies, colliders, and materials. I think you could solve a lot of your collision issues by manipulating the friction and bounciness parameters of the physics material.

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

20 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

Related Questions

How to check place on empty for placement polygon Rigidbody2d? 0 Answers

useAutoMass and child objects 1 Answer

Can a collider be solid along one axis only? 2 Answers

Trigger is not executed 1 Answer

Collision2D.relativeVelocity should be 0? 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