• 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
Question by KetihG28 · Apr 14, 2016 at 01:01 PM · rigidbodyvelocityaddforceupdate functionkeydown

Rigidbody Addforce cancels out Rigidbody velocity..maybe?

Hello everyone,

I like to think of myself of someone who is a little bit above beginner when it comes to coding/scripting in Unity. The problem that I'm having may be simple but I can not find the problem that is causing the headache. I've used Rigidbody.AddForce for causing force to happen when the player jumps and then I use that again to cancel out the initial jump AddForce. (works fine) The problem is once the gameobject is back on the ground, he seems to be moving in "place". I will break down what my set looks like. I apologize in advance if this is too wordy.

Screen0.png ( I did not add image since I'm limited to 2 image attachments)

The GameObject is simply running left and right with a keydown command that I set up in the Update function.

//Apply velocity to the rigidbody if statement is true

if (Input.GetKeyDown(KeyCode.D) && isGrounded == true ) { rbody.velocity = new Vector3(-5f, 0, 0); transform.localRotation = Quaternion.LookRotation(new Vector3(-180f, 0, 0)); }

     if (Input.GetKeyDown(KeyCode.D) && inAir == true)
     {
         rbody.velocity = new Vector3(-5f, 0, 0);
         transform.localRotation = Quaternion.LookRotation(new Vector3(-180f, 0, 0));
     }

     if (Input.GetKeyDown(KeyCode.A) && isGrounded == true)
     {
         rbody.velocity = new Vector3(5f, 0, 0);
         transform.localRotation = Quaternion.LookRotation(new Vector3(180f, 0, 0));
     }

     if (Input.GetKeyDown(KeyCode.A) && inAir == true)
     {
         rbody.velocity = new Vector3(5f, 0, 0);
         transform.localRotation = Quaternion.LookRotation(new Vector3(180f, 0, 0));
     }

Of course I have a keyup command to cancel those out so I will spare adding that info. But the above code is working fine. (This picture also points out the Capsue collider, a "Trigger" collider that I use to detect if the player is on the ground or not. I do have a raycast setup but did not see a difference whether I used that or a "trigger". I will spare the OnTriggerEnter & OnTriggerExit functions since the do not effect the velocity or addforce.

Screen1.png

The player is having negative AddForce effects to cancel out the AddForce that was used for jumping. alt text

//If the player is in the air X amount of time perform gravity force if (airTimer >= 35) { rbody.AddForce(new Vector2(0, gravity * 6));

     }

Screen2.png

The GameObject has landed and I'm trying to move it again with rigidbody.velocity but for some reason it seems as if the GameObject is running into a invisible wall.

alt text

I'm not sure if the Update function, where I have my keydown command, is not playing nicely with my AddForce function which is called from a public void Jump() function. Once the game object has reached a certain height the AddForce is called in the Update function to cancel out the initial force from the jump function. The Rigidbody.velocity, which is also in the Update function, no longer works. I hope this was not too much detail but I appreciate anyone who can help me get passed this hurdle.

screen1.png (70.5 kB)
screen2.png (60.6 kB)
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

45 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

Related Questions

Rigidbody movement conflict? 1 Answer

rigidbody velocity on x axis and addforce on y axis ?? 1 Answer

Stopping a rigidbody immediately 2 Answers

Velocity powered rigidbody on a moving platform without parenting. 3 Answers

Is there a formula I can use to calculate force needed to achieve a certain velocity? 2 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