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 johnryan2242 · Nov 03, 2019 at 07:06 PM · rigidbody2dvelocityconstantforce

Maintaining Velocity When Applying a Constant Force 2D

I am trying to develop a platformer where the player can manipulate how they experience gravity with button presses. For example when pressing W their gravity becomes up and if they press D they are pulled to the right side of the screen. For changing gravity between up and down, I just change the rigidbody's gravity scale from 1 to -1. When changing between left and right I set the gravityscale to 0 and use a ConstantForce2D to apply a constant force equal to gravity to either the right or left. The problem I am running into is that whenever I apply these horizontal forces, the velocity of my player is immediately cancelled out. So in the case that the player is falling but then changes their gravity to the right, instead of keeping their current vertical velocity and accelerating right as I would like to happen, they suddenly stop falling and begin moving right. Is there any way to get around this? I've pasted the relevant code below.

     private void Lash()
     {
         Vector2 currentVelocity = myRigidBody.velocity;
         //if (!isLashing)
         //{
         //    myRigidBody.gravityScale = 1f;
         //    myConstantForce2D.force = new Vector2(0f, 0f);
         //}
         if (!Input.GetKey(KeyCode.LeftShift))
         {
             if (Input.GetKeyDown(KeyCode.W))
             {
                 myConstantForce2D.force = Vector2.zero;
                 isHoriz = false;
                 isLashing = true;
                 if (floor)
                 {
                     myRigidBody.gravityScale = -1f;
                     myAnimator.SetTrigger("floorToCeiling");
                 }
                 else if (ceiling)
                 {
                     return;
                 }
                 else if (rightWall)
                 {
                     myRigidBody.gravityScale = -1f;
                     myConstantForce2D.force = Vector2.zero;
                     Debug.Log(myConstantForce2D.force);
                     myAnimator.SetTrigger("rightToCeiling");
                 }
                 else if (leftWall)
                 {
                     myRigidBody.gravityScale = -1f;
                     myConstantForce2D.force = Vector2.zero;
                     myAnimator.SetTrigger("leftToCeiling");
                 }
             }
             else if (Input.GetKeyDown(KeyCode.S))
             {
                 isLashing = false;
                 isHoriz = false;
                 if (floor)
                 {
                     return;
                 }
                 else
                 {
                     CancelLashings();
                 }
 
             }
             else if (Input.GetKeyDown(KeyCode.D))
             {
                 if (floor)
                 {
                     isLashing = true;
                     isHoriz = true;
                     myConstantForce2D.force = Vector2.right * (myRigidBody.mass * 9.81f);
                     myAnimator.SetTrigger("floorToRight");
                     myRigidBody.gravityScale = 0f;
                     myRigidBody.velocity = currentVelocity;
 
                 }
                 else if (ceiling)
                 {
                     isLashing = true;
                     isHoriz = true;
                     myConstantForce2D.force = Vector2.right * (myRigidBody.mass * 9.81f);
                     myAnimator.SetTrigger("ceilingToRight");
                     myRigidBody.gravityScale = 0f;
                     myRigidBody.velocity = currentVelocity;
 
                 }
                 else if (rightWall)
                 {
                     isLashing = true;
                     isHoriz = true;
                     return;
                 }
                 else if (leftWall)
                 {
                     CancelLashings();
 
                 }
 
             }
             else if (Input.GetKeyDown(KeyCode.A))
             {
                 if (floor)
                 {
                     isLashing = true;
                     isHoriz = true;
                     myConstantForce2D.force = Vector2.left * (myRigidBody.mass * 9.81f);
                     myAnimator.SetTrigger("floorToLeft");
                     myRigidBody.gravityScale = 0f;
                     myRigidBody.velocity = currentVelocity;
                 }
                 else if (ceiling)
                 {
                     isLashing = true;
                     isHoriz = true;
                     myConstantForce2D.force = Vector2.left * (myRigidBody.mass * 9.81f);
                     myAnimator.SetTrigger("ceilingToLeft");
                     myRigidBody.gravityScale = 0f;
                     myRigidBody.velocity = currentVelocity;
                 }
                 else if (rightWall)
                 {
                     CancelLashings();
 
                 }
                 else if (leftWall)
                 {
                     isLashing = true;
                     isHoriz = true;
                     return;
                 }
 
             }
             else if (Input.GetKeyDown(KeyCode.Q))
             {
                 CancelLashings();
             }
         }
 
     }

The animator triggers are for rotating the player to be properly oriented with the direction of gravity. 'Lashing' Is the name of the magic system I am implementing in this game. Any advice would be greatly appreciated.

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
0

Answer by xxmariofer · Nov 04, 2019 at 03:02 PM

dont set the forces

 myConstantForce2D.force = Vector2.right * (myRigidBody.mass * 9.81f);

just add them

 myConstantForce2D.force += Vector2.right * (myRigidBody.mass * 9.81f);
Comment
Add comment · Show 5 · 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 johnryan2242 · Nov 05, 2019 at 01:56 AM 0
Share

Unfortunately I still run into the same issue when I implement this change

avatar image xxmariofer johnryan2242 · Nov 05, 2019 at 08:21 AM 0
Share

where are you applying the constant force in the code?

avatar image johnryan2242 xxmariofer · Nov 07, 2019 at 06:46 PM 0
Share

Within the update method. From reading around I have come to understand that FixedUpdate is better / necessary for applying physics updates, should I start by making this change?

Show more comments

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

190 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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 player slower on ground than in air 1 Answer

Unity Rigidbody2D bug 0 Answers

Collision detection for the next frame 1 Answer

rigidbody2D's velocity not working 1 Answer

RigidBody2D.AddForce() horizontally is not working! 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