Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 SpatenDeamon · Feb 03, 2021 at 03:52 PM · movementrigidbodyvelocityrigidbody physics

How to make rigidbody movement without changing or clamping the Velocity?

I try to make a physic based rigidbody fps controller, but most fps controller changing the velocity like this:

 rb.velocity = new Vector3(move.x, rb.velocity.y, move.z);

If you change the velocity directly like this, the controller won't react to something like boost pads or any other changes to the velocity. Is there any other way to move the controller without changing or clamping the actually Velocity?

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

Answer by afraism · Feb 03, 2021 at 06:50 PM

try this:

 void Update()
     {
         // Move the object forward along its z axis 1 unit/second.
         transform.Translate(Vector3.forward * Time.deltaTime);
 
         // Move the object upward in world space 1 unit/second.
         transform.Translate(Vector3.up * Time.deltaTime, Space.World);
     }


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 hamerilay · Feb 03, 2021 at 11:00 PM

The answer is to add velocity instead of setting the velocity, to do this in the FixedUpdate put:

         if (Mathf.Abs(rb.velocity.x) < maxSpeed)
         {
             rb.velocity += new Vector3(move.x * speed, 0, 0);
         }
         if (Mathf.Abs(rb.velocity.z) < maxSpeed)
         {
             rb.velocity += new Vector3(0, 0, move.z * speed);
         }

use "Mathf.Abs()" to turn any number to a positive number.

Add these variables:

     public float speed;
     public float maxSpeed;
     public Rigidbody rb;
     Vector3 move;

In the Update function put this:

 move = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));

This code will check if the velocity is under the max speed and if it is true we add some velocity, if you want to boost the player just add or set the velocity. Here is an example to give a boost in the direction the player is going:

         if (Input.GetKeyDown(KeyCode.Space))
         {
             rb.velocity += rb.velocity * 1.5f;
         }

Final code:

     public float speed;
     public float maxSpeed;
     public Rigidbody rb;
     Vector3 move;
 
     private void Update()
     {
         move = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
 
         if (Input.GetKeyDown(KeyCode.Space))
         {
             rb.velocity += rb.velocity * 1.5f;
         }
     }
 
     private void FixedUpdate()
     {
         if (Mathf.Abs(rb.velocity.x) < maxSpeed)
         {
             rb.velocity += new Vector3(move.x * speed, 0, 0);
         }
         if (Mathf.Abs(rb.velocity.z) < maxSpeed)
         {
             rb.velocity += new Vector3(0, 0, move.z * speed);
         }
     }

I used speed: 0.5, maxSpeed: 5 and set the rb to the rigidbody on the player.

I hope this helped you!

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 AbandonedCrypt · Feb 03, 2021 at 07:07 PM

Unity has a nice physics implementation to apply forces to a rigidbody: Rigidbody.AddForce


You can choose between multiple types of applying force such as constant force or impulse. You can monitor the velocity and adjust the force you apply accordingly. That way you can still have external elements apply force (think boost pads) properly.


Edit: To not make your character fall over and act all weird, you can freeze rotation on all axis in the rigidbody constrains


Previous repliers answer is basically just transform manipulation and not physics based movement controlling.

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

203 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

Related Questions

rigidbody It's going up and bounces when is moving 1 Answer

Player movement not working with unity 5 1 Answer

Rigidbody movement with turns for a one wheeled motor cycle. 0 Answers

Rigidbody.velocity and Time.deltaTime 3 Answers

Bug, while checking if rigidbody is moving 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