• 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
5
Question by ShroomWasTaken · Jul 25, 2015 at 03:53 PM · c#unity 5rigidbodycharacter

Rigidbody player keeps moving after AddForce has stopped?

So, i have my player setup, there's just one problem now. When i press my vertical/horizontal inputs, and then release them the rigibody doesn't stop moving. It keeps moving for an unlimited amount of time. Any fixes for this? Code :

     using UnityEngine;
     using System.Collections;
 
     public class CharacterMovement : MonoBehaviour {
 
     Rigidbody rigidbody3D;
     public float speed = 4;
 
     void Start () 
     {
 
         rigidbody3D = GetComponent<Rigidbody>();
 
     }
     
     void FixedUpdate () 
     {
 
         // Checks for user inputs.
         float horizontal = Input.GetAxis("Horizontal");
         float vertical = Input.GetAxis("Vertical");
 
         Vector3 movement = new Vector3(horizontal, 0, vertical);
 
         // Makes the player move.
         rigidbody3D.AddForce(movement * speed / Time.deltaTime);
 
 
         // Limits the speed of the rigidbody.
         if (rigidbody3D.velocity.magnitude > speed)
         {
             rigidbody3D.velocity = rigidbody3D.velocity.normalized * speed;
         }
 
         
 
     }
 }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
8
Best Answer

Answer by ShroomWasTaken · Jul 25, 2015 at 09:15 PM

Fixed it by adding a drag value to my rigidbody!

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 AbdullahSafaan · Jan 12, 2017 at 03:38 AM 0
Share

I tried making a value but I failed :(, and i fixed it by this code i know it's weird and not professional at all, But it fixed it. Still = new Vector3 (0.0000000001f, 00000000001f, 00000000001f); GetComponent().AddForce(Still); Please can you tell me how to fix it in a way better than this.

avatar image
3

Answer by rahulkk · Jul 25, 2015 at 10:24 PM

The main problem is that Rigidbody.AddForce adds a force to a rigidbody that will make it keep going. You can think of it like throwing a ball. When you throw the ball, your hand adds force to the ball. Even after the ball leaves your hand, it will keep going even though no more force is being added.

You can instead use Rigidbody.MovePosition or simply set the position of the rigidbody if you don't want your rigidbody to keep going even after the movement keys are released. Another option is to set the velocity property of the rigidbody. However, this can sometimes lead to unrealistic physics behavior if you set it every frame.

Another minor thing is that you should multiply by Time.deltaTime, not divide by it on line 26. That way there will be more force for a larger delta time and less force for a smaller delta time.

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 Rowland_Goddy-Worlu · May 31, 2021 at 03:35 AM

To control the movement and the force of the game object better use the "Rigidbody.Addforce."

This way you can control your player's movement and your player won't just go through other objects with the Rigidbody component. Using "Rigidbody.MovePosition", the game object just passes through other objects irrespective of the components attached to it.

     void Update()
     {
         MovePlayer();
 
     }
 
     private void MovePlayer()
     {
         float horizontalMovement =  Input.GetAxis("Horizontal") * speed;
         float verticalMovement = Input.GetAxis("Vertical") * speed;
         playerRb.AddForce(Vector3.forward * verticalMovement);
         playerRb.AddForce(Vector3.right * horizontalMovement);   
     }
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Collision Not Working 1 Answer

How to change the sprite and the animator in script to make a character selection? 2 Answers

Problem with jumping in fps game 1 Answer

Rotation of instantiated clone based on prefab, when using another object's rotation as parameter in instantiate method. Why is that so 1 Answer

rigidbody.AddForce() is making my character fall over 3 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