Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Willfree · Jan 15, 2019 at 10:33 AM · rigidbody2dcontrolsrigidbody.addforcerigidbody.velocityphyics

Using both RB Velocity and AddForce?

Hello, I am working on a 2D platformer and i use Rigidbody2D.velocity to move. It is easier to stop the player if i am not pressing a key and removes acceleration. But i wanted to push the player back when there is a collision with an enemy and because the velocity is set to 0 when no key is pressed, i can't use "AddForce", the player stops in air. So i was wondering if there is a solution to keep make the player stop instantly but keep the AddForce.

Here is my code :

 void Movement()
 {
     //Move
     float horizontal = Input.GetAxis("Horizontal");
     rb2D.velocity = new Vector2(horizontal * speed, rb2D.velocity.y);
     
     //Jump
     rb2D.AddForce(new Vector2(0, jumpForce), ForceMode.Impulse);
     
     //Getting pushed back by enemy (debug)
     if(Input.GetKeyDown(KeyCode.T))
         rb2D.AddForce(new Vector2(-pushForce, 0)ForceMode.Impulse);
 }
Comment
Add comment · Show 1
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 tormentoarmagedoom · Jan 15, 2019 at 10:38 AM 0
Share

Good day.

IF velocity = 0 is applied, the addforce will have no effect.

So you need to dont apply the velocity command during the time you want it to move back.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ma9ici4n · Jun 14, 2019 at 05:20 AM

This is probably a bit late, but to fix this you can make the player be unable to move for like one third of a second when getting knocked back.

      void Movement()
      {
          //Move
          float horizontal = Input.GetAxis("Horizontal");
          if (beingknocked == true)
              rb2D.velocity = new Vector2(horizontal * speed, rb2D.velocity.y);
          
          //Jump
          if (beingknocked == false)
              rb2D.AddForce(new Vector2(0, jumpForce), ForceMode.Impulse);
          
          //Getting pushed back by enemy (debug)
          if(Input.GetKeyDown(KeyCode.T))
              rb2D.AddForce(new Vector2(-pushForce, 0)ForceMode.Impulse);
              beingknocked = true
              yield return new WaitForSeconds(0.3);
              beingknocked = false
      }

This way the player gets stunned when knocked back so the rb.velocity command won't interfere with the addforce.

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

166 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

Related Questions

rigid.velocity.x = 0 for unknow reason 0 Answers

2D Rigidbody Velocity Being Weird 1 Answer

my 2d character isnt jumping with constant velocity on applying force 1 Answer

OnCollision2D not accessing rigidbody velocity 0 Answers

How to throw an object towards a hit.point in first person? 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