Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
2
Question by gumboots · Aug 30, 2014 at 02:09 AM · collisioncharactercontrollerphysics2dtopdown

2D Top Down Circular Collisions (Without a Rigidbody)

Hi there,

I'm looking to replicate the CharacterController.Move function, using a top down 2D object. Unfortunately I can't use a Rigidbody2D, as my movement is being synced over an authoritative network and it has been causing jittery movement for the player. I can't just use 3D physics because a lot of our gameplay is reliant on functionality using Sprites.

Right now I'm doing eight OverlapCircles (one in each direction) and removing the movement direction from the input for those that return positive, but this is only preventing head on and diagonal collisions. Using a controller allows quite a bit of cheeky maneuverability, and even with a keyboard you can squeeze through gaps smaller than your collider.

The other restriction I have is that my level is made up of many, compound edge colliders (spread across numerous GameObjects), so waiting for the player to be in an illegal position and then spitting them out isn't feasible.

Here is my current Move() function. CharacterCollisionPoint2D is just an object with a position (relative to the player), and two booleans to keep track of it.

 public void Move(Vector2 movement){
     if(movement == Vector2.zero){ return;}
     Vector2 currentPosition = _transform.position;
     Vector2 targetPosition = currentPosition + movement;
     Vector2 targetDirection = targetPosition - currentPosition;
     float moveDistance = targetDirection.magnitude;
 
     Vector2 desiredMotion = targetDirection * moveDistance;
 
     foreach(CharacterCollisionPoint2D collider in colliders){
         bool movingAwayX = (movement.x < 0 && collider.position.x > 0) || (movement.x > 0 && collider.position.x < 0);
         bool movingAwayY = (movement.y < 0 && collider.position.y > 0) || (movement.y > 0 && collider.position.y < 0);
 
         if(movingAwayX || movingAwayY){
             collider.isActive = false;
             continue;
         }
 
         collider.isActive = true;
 
         if(Physics2D.OverlapCircle(currentPosition + collider.position, 0.15f, nonPlayer)){
             collider.isColliding = true;
             Vector2 directionIntoCollision = (currentPosition + collider.position - currentPosition).normalized;
             desiredMotion -= directionIntoCollision * (Vector2.Dot (desiredMotion, directionIntoCollision));
         } else {
             collider.isColliding = false;
         }
     }
 
     desiredMotion = desiredMotion.normalized * moveDistance;
 
     _transform.Translate(desiredMotion);
 }

And here is a package with a very, very basic scene and Player setup (Click the player to see the debug lines):

https://dl.dropboxusercontent.com/u/6612485/2d-topdown-charactercontroller.unitypackage

I'm open to any alternative methods. I was previously using a Rigidbody2D with Rigidbody2D.MovePosition while setting up the movement, and it was working perfectly, unfortunately it just isn't possible to smoothly control over the network.

Any and all help appreciated!

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
3
Best Answer

Answer by gumboots · Sep 04, 2014 at 04:22 AM

We ended up getting an excellent freelancer to solve this, and it works perfectly. I've put the final result up on GitHub for anyone in the same situation:

https://github.com/adamscoble/TDCharacterController2D

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

2 People are following this question.

avatar image avatar image

Related Questions

character bumps when going between overlapping or side by side colliders 0 Answers

Object following a path and colliding with other objects with physics. 1 Answer

Any way to ignore collision between rigidbodies and colliders/character controllers? 1 Answer

Character Controller meets Rigidbody 1 Answer

Check if player is on track 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