• 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 TheCallipo · Jan 18, 2017 at 09:20 AM · movementrigidbody2daddforcey

[2D] Can't AddForce to x but y works fine.

Hey guys. Noob problems incoming :D

So I'm working on this 2D Game and wanted to add something like a sliding-movement. Played Titanfall 2 recently and yeah... ^^

I'm stuck at the basic functionality. I want to AddForce to my RigidBody2D Character on the x axis by GetButton.

The function looks basicaly the same like my Jump() function. Only thing is that I switched the variables position to x and set y to 0:

Jump() Function(everything works here. the character gets the force correctly.):

    void Jump()
     {
         if (isGrounded)
         {
             canJumpAgain = true;
             animator.SetTrigger("Jump");
             rigidBody.velocity = new Vector2(rigidBody.velocity.x, 0);
             rigidBody.AddForce(new Vector2(0, jumpSpeed), ForceMode2D.Impulse);
         }
         else if(!isGrounded)
         {
             if (canJumpAgain)
             {
                 animator.SetTrigger("DoubleJump");
                 rigidBody.velocity = new Vector2(rigidBody.velocity.x, 0);
                 rigidBody.AddForce(new Vector2(0, jumpSpeed), ForceMode2D.Impulse);
                 canJumpAgain = false;
             }
         }
     }

Kick() Function(I don't get any force from anywhere.):

     void Kick()
     {
         if (isGrounded)
         {
             rigidBody.AddForce(new Vector2(jumpSpeed, 0), ForceMode2D.Impulse);
             attackController.currentDamage = attackController.normalKickDamage; 
             animator.SetTrigger("Kick");
             attackController.isKicking = true;
         }
         else if (!isGrounded)
         {
             animator.SetTrigger("JumpKick");
             attackController.currentDamage = attackController.critKickDamage;
             attackController.isKicking = true;
         }
     }

Here again the difference between the AddForce on Jump():

 rigidBody.AddForce(new Vector2(0, jumpSpeed), ForceMode2D.Impulse);

and AddForce on Kick():

 rigidBody.AddForce(new Vector2(jumpSpeed, 0), ForceMode2D.Impulse);

I really don't get it.

My GameObject Hierarchy:

  • Player1 (the parent containing the previous scripts, Rigidbody2D and a Collider2D for isGrounded purposes)

  • -Skin (has an animator controlling the child sprites)

  • --Head

  • --Body (contains collider set as a trigger for incoming damage)

  • --RightArm

  • --LeftArm (contains collider set as a trigger for outgoing damage)

  • --RightFoot

  • --LeftFoot

what could cause a hurdle that don't let my player1 game object move to the side?

isGrounded works fine so no problem here.

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

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

Answer by TheCallipo · Jan 18, 2017 at 06:43 PM

Fixed it by removing any changes in velocity I made in code.

"In most cases you should not modify the velocity directly, as this can result in unrealistic behaviour."

-Unity Documentation

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 Cyborg_X · Sep 15, 2019 at 11:56 AM 0
Share

I am very late but you are a life saver!

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

107 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

Related Questions

2D Top-Down Character: How to make my character move using physics? 1 Answer

Using rb.velocity and Add Force on the same gameobject? 0 Answers

How to stop the character moving after the button is pressed, using Rigidbody2D()..AddForce? 1 Answer

2D : How to overwrite rigidbody velocity when "skill" input pressed ??? 0 Answers

Car game and simple drift system with rigidbody.velocity 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