• 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
1
Question by Yunitici · Dec 23, 2013 at 11:22 PM · 2dmovementphysics

2d topdown physics

What is the best practice for moving player in a 2d topdown game?

This is my movement script attached to player;

 public class MovementScript : MonoBehaviour {
 
     public Vector2 speed = new Vector2(25, 25);
     
     private Vector2 movement;
     
     void Update()
     {
         float inputX = Input.GetAxis("Horizontal");
         float inputY = Input.GetAxis("Vertical");
 
         movement = new Vector2(speed.x * inputX, speed.y * inputY);
 
         if (inputX != 0 && inputY != 0) {
             movement *= 0.707106781f;
         }
     }
     
     void FixedUpdate()
     {
         rigidbody2D.velocity = movement;
         //which one is preferrable?
         //rigidbody2D.AddForce (movement);
     }
 }

Also, there are two players in the scene. Both are cylinders with Circle collider and rigidbody 2d. When a player hits the other one, there is a strange behaviour that it feels like two players become together.

For example, i am going right and when i hit the other player on the left and continue to go right, we both go right. Thats okay, however while going right, if i press the up button, the other player goes up too. It feels like two players collide and stick together. This is not normal in 2d. What can i do to solve this?

If you need anything, please tell me and i will post here (settings, etc.)

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 Josh707 · Dec 24, 2013 at 12:04 AM 0
Share

Do you want the players to just pass through eachother? I don't know if the new 2D framework uses layer-based collision but you can try throwing them on different layers.

If not, perhaps use something like bounds.Intersects on both players to deter$$anonymous$$e if they're inside eachother, and apply some pushing force to each player to keep them apart. Another idea is to predict collisions before you move the player, if there is a wall/player/etc. don't execute the movement code.

For the physics question, I'm not sure how it should be handled in 2D. I don't see how a rigidbody would be useful if there is no gravity involved, do you just want the rotation and collision detection?

avatar image Yunitici · Dec 24, 2013 at 08:22 AM 0
Share

I want the objects to collide however they should not act like they stick to each other when they collide. I found out that is because friction. When i set the friction to 0. The problem got solved.

A rigidbody is sometimes useful when there is no gravity. The best example is a billiard game. You need all the physics rules (angular momentum, forces, etc.) however you dont need gravity.

Thanks.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Yunitici · Dec 24, 2013 at 09:24 AM

Thanks for answers. I have investigated this issue and here are my findings in case anyone goes through same problems:

  • The code is okay.

In FixedUpdate:

  rigidbody2D.velocity = movement;

is preferable if you want to change the direction of character as soon as the user presses a direction key. However if you want to achieve real physics (eg. momentum) you should use;

 rigidbody2D.AddForce (movement);

Using AddForce applies the force to the object. The object already has a direction and speed, and by using AddForce you add another force to the objects's current momentum. So you can achieve more realistic physics this way. However the object will not immediately respond to the direction buttons pressed, because it has a momentum.

  • Two objects that are dragging each other in an undesirable way is the result of friction. Add a 2D Physics Material to Rigidbody component of that object and set Friction to 0 for the physics material. You will see that objects will no longer act like they are sticking to each other when they hit each other.

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

Answer by PlasmaByte · Dec 24, 2013 at 12:57 AM

You could freeze the position for one of the axis under the inspector for all of your objects. That should limit them to moving in 2D.

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 Eric5h5 · Dec 24, 2013 at 08:32 AM 2
Share

2D physics can only move in 2D anyway, that's kind of the point of 2D physics....

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

thruster-like 2d movement 2 Answers

Character still walking when no key is already pressed 1 Answer

How can I make 2D movement less jerky on a controller, with velocity and such? 0 Answers

How can I make 2D movement the same on controller and keyboard? 0 Answers

Having problems with animation and movement of 2D Character 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