Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 ordaskal · Feb 10, 2014 at 10:44 PM · c#2dphysicsrigidbodyaddforce

AddForce with 2D Character

I got a character with a 2D rigidbody, the game is an infinite runner. Traps are coming from the right to the left and the player needs to jump above them. Now, I want that if the player hits a trap he will be knocked back a specific distance (that I can modify).

I got the collision part working fine but how can I make the trap to push back the player? am I suppose to use AddForce here? If you answer please tell me if I need to put rigidbody2D on the traps or anything important in addition to your code.

(this is in C#, 2D mode as I mentioned).

Comment
Add comment · Show 2
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 DajBuzi · Feb 10, 2014 at 10:47 PM 0
Share

Traps are triggers?

avatar image highpockets · Feb 10, 2014 at 11:53 PM 0
Share

Well if you have the collision part working fine and all you need is to hit the player backwards, then, assu$$anonymous$$g that you want to push the player back on the x axis (-x) rigidbody.AddForce( -10, 0 )... Since you have the collision part working fine, I wouldn't worry about adding a rigid body to your traps

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by MileSplit · Feb 11, 2014 at 12:52 AM

Yes you are supposed to use add force. It works almost the same way as it does in 2d but it ignores the z axis. Because of that you need to use a Vector2 instead of a Vector3. Also you would use rigidbody2D.AddForce, instead of the rigidbody.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
avatar image
1

Answer by ordaskal · Feb 11, 2014 at 03:30 PM

It Doesn't work, am I suppose to put the script on my traps or on the 2D character? Right now I got on both and still nothing happens. here is the script on the trap:

 void OnTriggerEnter2D(Collider2D other) {
     if (other.tag == "Player")
     {
         rigidbody2D.AddForce(-transform.right * 500);
     }
 }

and the script on the 2D character:

     void OnTriggerEnter2D(Collider2D other) {
         if (other.tag == "TrapLevel1")
         {
             rigidbody2D.AddForce(-transform.right * 500);
                       
         }
 }
Comment
Add comment · Show 3 · 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 DajBuzi · Feb 11, 2014 at 03:33 PM 0
Share

There is no such thing as transform.right

Its Vector3.right or Vector2.right

The second thing that you're doing wrong is if your trap has rogidbody attached it will shoot itself to the direction specified in your code.

ITrap: if(col.tag == "player") col.rigidbody2D.AddForce( -Vector2.right * power);

avatar image highpockets · Feb 11, 2014 at 05:27 PM 0
Share

You shouldn't answer yourself with a comment. When you have more questions, just comment

avatar image AibNihan · May 03, 2017 at 08:56 PM 0
Share

it's work for me Thank you so much for the information

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

22 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

Related Questions

AddForce vs Velocity issues with Rigidbody2D 2 Answers

Get result (force & torque) of AddForceAtPosition? 2 Answers

Make a player jump tile 1 Answer

2D floating object 2 Answers

rigidbody2D.addforce in collider 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