• 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 Noztradamuz · Jun 24, 2013 at 09:37 PM · c#rigidbodyvector3force

Apply vertical Force then return it to Zero on release Key

Hi, im having some troubles I have a swimming style game where the player moves forward, up and down, i'm applying forces to a rigidbody in order to move the player more naturally the aceleration is working fine, the thing is when i want to move up or down the player moves but the velocity of the rigidbody remains, i mean it keeps moving vertically, but i want it to only move a little bit more when the user releases the key, not to keep moving vertically at a constant speed.

Here's the code im applying to the movement, everything is succesfully called from FixedUpdate() and input from Update()

     void FixedUpdate ()
     {
         if (isPlayer)
         {
             if (up && !down)
                 Subir();
 
             if (down && !up)
                 Bajar();
 
         }
     }
 
  public void Subir()
     {
         rigidbody.AddForce(Vector3.up * VerticalVelocity,ForceMode.Force);
     }
 
     public void Bajar()
     {
         rigidbody.AddForce(Vector3.down * VerticalVelocity,ForceMode.Force);
     }

i'm changing the up and down variables from the Input on the update. i think is not necessary to show it, the problem is if there's some way to slowly decelerate the Y velocity of the rigidboy to Zero when the user releases the key. I've already tried with different ForceMode, Impulse,Acceleration,Force, etc... without any result.

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 robertbu · Jun 26, 2013 at 01:40 AM 0
Share

You can handle this a couple of different ways. The easiest would be to up the drag (default is 0) in the inspector for the rigidbody. You can also directly manipulate the rigidbody.velocity. Something like:

 if (!up && !down)
   rigidbody.velocity *= 0.9;
avatar image Noztradamuz · Jun 26, 2013 at 03:33 PM 0
Share

Hi, thanks, i managed to do this in another way, without directly altering the velocity of the rigidbody, it seems more natural, here's the code:

             if (rigidbody.velocity.y != 0)
         {
             if (!up && !down)
             {
                 if (rigidbody.velocity.y > 0)
                 {
                     rigidbody.AddForce(new Vector3(0, -rigidbody.velocity.y*2f, 0));
                 }
                 else if (rigidbody.velocity.y < 0)
                 {
                     rigidbody.AddForce(new Vector3(0, rigidbody.velocity.y*-2f, 0));
                 }
             }
         }

anyway thanks.

avatar image Em3rgency · Jun 26, 2013 at 03:37 PM 0
Share

I'm glad you solved your problem, though I don't get it. You add/remove force to the y axis velocity at a value of the x axis velocity? O.o ANYWHO, if your problem is gone, mark the question as closed on your first post :) Cheers!

avatar image Noztradamuz · Jun 26, 2013 at 04:48 PM 0
Share

LOL! I just made a mistake there it was rigidbody.velocity.y! not X! but it works

avatar image Noztradamuz · Jun 26, 2013 at 04:52 PM 0
Share

That was actually the final code, thanks! and how can i close the question?

avatar image Em3rgency · Jun 26, 2013 at 04:58 PM 0
Share

At the bottom of your first post, right below the tags, there is a small close button.

2 Replies

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

Answer by Noztradamuz · Jun 26, 2013 at 04:20 PM

Hi, thanks, i managed to do this in another way, without directly altering the velocity of the rigidbody, it seems more natural, here's the code:

              if (rigidbody.velocity.y != 0)
         {
             if (!up && !down)
             {
                 if (rigidbody.velocity.y > 0)
                 {
                     rigidbody.AddForce(new Vector3(0, -rigidbody.velocity.y*2f, 0));
                 }
                 else if (rigidbody.velocity.y < 0)
                 {
                     rigidbody.AddForce(new Vector3(0, rigidbody.velocity.y*-2f, 0));
                 }
             }
         }

anyway thanks.

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 Jonathon82931 · Aug 15, 2013 at 07:07 PM 0
Share

this helped me solve a problem in my code thankyou

avatar image
0

Answer by Em3rgency · Jun 26, 2013 at 07:07 AM

As robertbu said, the general idea behind his code snippet should work, however I'd add some safety checks.

 if (!up && !down)
   {
     //No point in doing this if the body is already still   
     if(rigidbody.velocity != 0)
       {
         //Multiplying by 0.9 will never result in 0, the value
         //will just get smaller and smaller. So we set it to 0
         //manually when its small enough. In this case 2.
         if(rigidbody.velocity > 2)
           rigidbody.velocity *= 0.9;
         else
           rigidbody.velocity = 0;
       }
   }
   
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

17 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

Related Questions

object move towards to mouse position 0 Answers

Set Velocity at relative position. 2 Answers

How to hit gameobjects to go into the opposite direction of hit? 1 Answer

How to apply in opposite direction to transform? 1 Answer

Move left etc. according to where player faces 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