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 /
This question was closed Nov 25, 2015 at 07:08 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by Aggrojag · Apr 11, 2014 at 06:22 AM · movementrigidbody.addforce

Adding Force to Rigidbody2D in C#

I've only recently started programming, so I hope this is an easy fix. Anyways, I started out with my script functioning in the 2D sections of unity. I originally could not get rigidbody2D.AddForce(x,y); to work, so as I test I moved it all over to 3D for rigidbody3D.AddForce(x,y,z); and it worked! At this point, I'm entirely unsure of what the problem is. Here is the portion of the script using this.

  //If player is moving and if player is not moving too fast;
  if (Input.GetButton ("Horizontal") ) {

     //Set horizontal movement;
     if(velocityTracker < velocityCap ){

     rigidbody2D.AddForce(speed * horizontal * Time.deltaTime * 100 * boostX, 0f);

     Debug.Log (rigidbody2D.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

1 Reply

  • Sort: 
avatar image
2
Best Answer

Answer by Tomer-Barkan · Apr 11, 2014 at 06:26 AM

If you only changed the code to rigidbody3d and it worked, then this means that your gameobject has a rigidbody3d component, and you can't use rigidbody2d commands.

In the unity editor, select your GameObject, and check which component it has (in the inspector). If it is a "rigidbody" component, then remove it, and then add a rigidbody2d component instead (under component -> physics 2d -> rigidbody 2d).

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 Aggrojag · Apr 11, 2014 at 06:41 AM 0
Share

The script wont even compile, not to mention I have changed the GameObject's rigidbody to 2D.

"Error CS1501: No overload for method 'AddForce' takes '2' arguments (CS1501) (Assembly-CSharp)"

I suppose I should have included this.alt text

rigidbody2d.png (6.9 kB)
avatar image Tomer-Barkan · Apr 12, 2014 at 11:40 AM 2
Share

That's because AddForce needs to receive a Vector2, not two parameters.

Try:

     rigidbody2D.AddForce(new Vector2(speed * horizontal * Time.deltaTime * 100 * boostX, 0f));
avatar image Like A Sir Tomer-Barkan · Nov 25, 2015 at 06:14 PM 0
Share

Thank you sir! Your answer really helped me !

Follow this Question

Answers Answers and Comments

23 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

Related Questions

Multiple Cars not working 1 Answer

Help with movement script 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Making a bubble level (not a game but work tool) 1 Answer

When I look down and move forward, I fly upwards 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