• 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 Johan 4 · Mar 05, 2011 at 03:33 PM · rigidbodyforce

Rigidbody.addforce the force keeps on adding.

I have this piece of code

rigidbody.AddForce(new Vector3(Input.GetAxis("Horizontal") * 2, 0, 0));

The problem is that even if I release the Horizontal button, it will still keep adding force more and more.

The code is in update function, but it still shouldn't be happening because when axis is not pressed, it outputs 0 right?

Ok I found the problem, I debug logged the input axis and when you press it, it goes to 1 but when you release it, it doesn't go back to 0, but how do I fix that

Comment
Add comment · Show 6
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 Joshua · Mar 05, 2011 at 03:49 PM 0
Share

Sounds correct to me.

avatar image Johan 4 · Mar 05, 2011 at 03:51 PM 0
Share

But it doesn't work correctly. I don't want to keep on adding force, I want to add force only when the input is pressed and die down when it's not pressed :(

avatar image Joshua · Mar 05, 2011 at 03:51 PM 0
Share

Actually, what is the 'new' doing there? Seems to me like it should be rigidbody.AddForce(Vector3.right* Input.GetAxis("Horizontal") * speed); Speed here is a var you still have to declare at top of your function though, and Vector3.right is short for Vector3(1,0,0)

avatar image Joshua · Mar 05, 2011 at 03:56 PM 0
Share

$$anonymous$$y bad, in JS it would look exactly the same without the new, and since I only know JS I assumed it was a mistake.

avatar image Johan 4 · Mar 05, 2011 at 04:00 PM 0
Share

Edited op with the problem

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by owenpat · Mar 05, 2011 at 04:47 PM

Look at topBar: Edit->ProjectSettings->Input. Then open Horizontal in the Inspector and check that Gravity isn't 0.

Horizontal normally ramps from -1 (leftArrow) to 1 (rightArrow) and automatically drifts to 0 when you let go. The "drift to zero" speed is gravity. If yours was somehow knocked to 0, your horizontal will stick as you described. Higher is a faster snap to 0 (default is 3.)

But, the way you are doing things, I'd not use GetAxis("Horizontal"). It's designed with auto-falloff to make the character coast to a stop when you let go, instead of snapping to a halt. Since you're using it for acceleration instead of speed, you don't need that. In other words, you're simulating someone who gradually pushes and releases the gas pedal.

I think just if(Input.GetKey("left")) rigidbody.addForce(-Vector3.right); will give better results and will solve the problem.

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.

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

No one has followed this question yet.

Related Questions

Realistic/No gravity spaceship movement? 1 Answer

How to make a RigidBody move in a certain direction 1 Answer

Applying force to a rigidbody 2 Answers

How can Player or Enemy Generate Force to Push Rigidbodies? 1 Answer

Manually Apply Cars Collision Response Force 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