• 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 /
  • Help Room /
avatar image
Question by Durstie · Sep 21, 2020 at 10:05 PM · rigidbodyrigidbody.addforcerigidbody.velocity

Accelerate Player with Rigidbody.AddForce

I'm making a third person project to learn Unity. I've imported a free humanoid model from the asset store and attached a player script to the model. In my FixedUpdate function, I apply a force to the model to make it move. Movement works. What I want to do now, is make it so that my player doesn't go from standing still to max speed instantly. I followed a tutorial and what it said makes sense, but it's not working. It said to apply a smaller force over many ticks to get the desired acceleration effect. Once masted, incorporate Time.deltaTime logic to really tweak the result.


What I have so far:

 private void FixedUpdate()
     {
         // If we are not grounded, we cannot move
         if (IsGrounded() && playerTryingToWalk)
         {
             // Calculate how fast we should be moving
             //    TargetVelocity will be a Vector3 (x, 0, y) where:
             //        x is either -1 or 1 depending on left or right
             //        y is either -1 or 1 depending on forward or backward
             Vector3 targetVelocity = new Vector3(moveVector.x, 0, moveVector.y);
             // Multiply the targetVelocity by the player's speed to get the real velocity we wish to achieve
             targetVelocity *= playerSpeed;
             // If we are pressing the boost button we are running, increase the target velocity again
             if (playerBoosting)
             {
                 targetVelocity *= playerRunMultiplier;
             }
 
             // Apply a force that attempts to reach our target velocity
             //        This is called in Start(): rb = GetComponent<RigidBody>();
             Vector3 velocity = rb.velocity;
             // Subtract our current velocity from our target velocity 
             //  to see how much more velocity we need to achiever our target
             Vector3 velocityChange = (targetVelocity - velocity);
             Debug.Log(velocity);
             Debug.Log(velocityChange);
             // Don't apply the full force needed in the first tick, 
             // stretch it out over however many ticks it takes
             //  Player Acceleration is 1.0f
             velocityChange.x = Mathf.Clamp(velocityChange.x, -playerAcceleration, playerAcceleration);
             velocityChange.z = Mathf.Clamp(velocityChange.z, -playerAcceleration, playerAcceleration);
             velocityChange.y = 0;
             // Finally, apply the force needed to accelerate
             rb.AddForce(velocityChange, ForceMode.VelocityChange);
         }
     }

The issue I'm having is that the code above does not push my player to the target velocity, it maxes out at whatever the playerAcceleration value is. As an example if my targetVelocity is 100 and my playerAcceleration is 10, my character velocity never goes above 10.


Something I noticed is that my two Debug.Log lines above always output the same thing.


Debug.Log(velocity); - Always outputs (0,0,0)

Debug.Log(velocityChange); - Always outputs (x, 0, y) where x and y are always +/- playerAcceleration depending on movement direction


Please help,

Dustin

Comment

People who like this

0 Show 0
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

0 Replies

· Add your reply
  • Sort: 

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

260 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 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 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 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

revolving a rigidbody around another MOVING rigidbody 0 Answers

Only limit player input and not external forces (Rigidbody 3D) 1 Answer

how do I use rb.addforce without momentum screwing with the direction of an object? 0 Answers

How to throw an object towards a hit.point in first person? 1 Answer

Hello guys, why i can not add this to my code? 1 Answer


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