Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 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
0
Question by unity_5120bee · Dec 18, 2017 at 04:05 AM · unity 2dvelocitygravityjumpingplatformer

Why does Gravity (Physics2D) produce different results for AddForce() versus velocity in player sprite?

Why does Gravity in Physics2D engine for Unity2D act different when these two following lines of codes are implemented alternately?

For example, I have attached to my player sprite a Player Controller C# script:

 private float speed = 500f;
 RigidBody2D playerChar = null;

And then to make my character walk:

 Vector2 vec = new Vector2 (Input.GetAxis("Horizontal"), 0);
 playerChar.AddForce(vec * speed);

And the Gravity is set to 50

Result 1: My character avatar falls down normally.

Meanwhile when I do:

 Vector2 vec = new Vector2 (Input.GetAxis("Horizontal"), 0);
 playerChar.velocity  = (vec * speed);

And the Gravity is still set to 50

Result 2: My character now takes a long time to fall (it slowly "floats" down).

Why is that?

Comment
Add comment · Show 1
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 Nikaas · Dec 18, 2017 at 09:53 AM 0
Share

Where is that code added?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Nikaas · Dec 18, 2017 at 09:59 AM

Without having all the information I assume the cause is that the code is re-applied multiple times. So in the second example the vertical velocity is periodically reset to 0, while in the first example the force adds velocity on top of the current velocity.

===

  • AddForce adds velocity on top the current one (sidenote: AddForce's effect strenght depends on mass).

  • While velocity property replaces/resets current velocity to the one that's passed.

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
0

Answer by aurelioprovedo · Dec 18, 2017 at 10:20 AM

In Result 2, you are setting the horizontal velocity AND the vertical velocity. Since the vertical velocity that you are providing is zero, it is stopping the object every frame.

Change your input code to this: float horiz = Input.GetAxis("Horizontal");

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

76 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

Related Questions

Trying to limit air velocity 2 Answers

Problem of gravity 1 Answer

Jumping a specific height using Velocity & Gravity 1 Answer

Gravity trouble: Falling slow, jumping fast 1 Answer

Unity2d Walking off platform throws off gravity 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