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 /
This question was closed Feb 06, 2016 at 09:40 PM by TommyJ for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by TommyJ · Feb 01, 2016 at 06:35 PM · uiphysicsphysics2d2d-physics

2D physics slow moving down

In my game I have a ball that moves around the screen, I am using this script to make it work, it has a 2DRigidbody attached. For some reason when I press down to move down, it moves down slowly as compared to everyother direction. Why is this happening?

         if(this.name == "redBall")
         {
             bool movingX = (Input.GetAxis("Horizontal") != 0);
             bool movingY = (Input.GetAxis("Vertical") != 0);
 
             float _xAccel = 0, _yAccel = 0;
 
             _xAccel = Input.GetAxis("Horizontal");        
             _yAccel = Input.GetAxis("Vertical");
 
             Vector2 vec = new Vector2(_xAccel, _yAccel);
             GetComponent<Rigidbody2D>().AddForce(vec * Speed);
             GetComponent<Rigidbody2D>().velocity = Vector2.ClampMagnitude(GetComponent<Rigidbody2D>().velocity, maxSpeed);
             float newX = GetComponent<Rigidbody2D>().velocity.x, newY = GetComponent<Rigidbody2D>().velocity.y;
 
             if (!movingX && newX > 0)
                 newX -= slipperiness;
             else if (!movingX && newX < 0)
                 newX += slipperiness;
 
             if (!movingY && newY > 0)
                 newY -= slipperiness;
             else if (!movingX && newY < 0)
                 newY += slipperiness;
 
             if (newX > 0 && newX < slipperiness)
                 newX = 0;
             
             if (newX < 0 && newX > (-slipperiness))
                 newX = 0;
             
             if (newY > 0 && newY < slipperiness)
                 newY = 0;
             
             if (newY < 0 && newY > (-slipperiness))
                 newY = 0;
 
             GetComponent<Rigidbody2D>().velocity = new Vector2(newX, newY);
         }

EDIT : I just figured out that if I use AddForce() I get this problem, but when I directly change the velocity it moves the same speed in all directions, but with changing it's velocity it doesn't react the same with other colliders in the scene the way it did with AddForce(). It does not bounce as much it kinda bounces a centimeter than sticks to it. It also speeds up very quickly to maxSpeed and doesn't need the part of the script that slows it down.

             Vector2 vec = new Vector2(_xAccel, _yAccel);
             //GetComponent<Rigidbody2D>().AddForce(vec * Speed);
             GetComponent<Rigidbody2D>().velocity = new Vector2(vec.x * Speed, vec.y * Speed);
             GetComponent<Rigidbody2D>().velocity = Vector2.ClampMagnitude(GetComponent<Rigidbody2D>().velocity, maxSpeed);
             float newX = GetComponent<Rigidbody2D>().velocity.x, newY = GetComponent<Rigidbody2D>().velocity.y;

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 Teravisor · Feb 06, 2016 at 08:43 PM 1
Share

else if (!movingX && newY < 0) newY += slipperiness;

Should be movingY

avatar image TommyJ Teravisor · Feb 06, 2016 at 09:40 PM 0
Share

how did i miss this. i thank you good sir

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Is there a standardized way for creating a 2D ball physics movement method? 0 Answers

Physics2D Overlapbox not working as aspected 0 Answers

Maintain equal physics over different UI sizes? 2 Answers

Game not working on Tizen 0 Answers

Should a physics material be added to the collider or the rigidbody? 2 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