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 Marscraft31415926 · Oct 03, 2021 at 11:27 PM · 3drigidbody.addforcerigidbody physics

Keeping gameobject at constant speed regardless of drag,Keeping constant force regardless of drag

So, I'm a newbie who just started Unity. I made a game where you just dodge obstacles with a cube, and everything is going fine until when I added a jump option. To keep it moving at one direction, I used AddForce every FixedUpdate on the z axis, and the player IS moving at a constant speed, but when it jumps, the player accelerates. I think it is because of the lack of drag.

Here is my script for PlayerMovement:

{ public Rigidbody rb;

 public float forwardForce = 700f;
 public float sidwaysForce = 100f;

 public Vector3 jump;
 public float jumpForce = 3.0f;

 public bool isGrounded;

 void Start()
     {
         rb = GetComponent<Rigidbody>();
         jump = new Vector3(0.0f, 2.0f, 0.0f);
     }

 void OnCollisionStay()
 {
     isGrounded = true;
 }
 void OnCollisionExit()
 {
     isGrounded = false;
 }
 void Update()
 {
     if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
     {

         rb.AddForce(jump * jumpForce, ForceMode.Impulse);
         isGrounded = false;
     }
 }
 void FixedUpdate()
 {
     rb.AddForce(0, 0, forwardForce * Time.deltaTime);

     if (Input.GetKey("d"))
     {
         rb.AddForce(sidwaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
     }

     if (Input.GetKey("a"))
     {
         rb.AddForce(-sidwaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
     }
     
     if (rb.position.y < -1f)
     {
         FindObjectOfType<GameManager>().EndGame();
     }
 }

} ,So, I'm a newbie that is trying to make a simple game where you just move a cube and dodge obstacles. the cube moves forwards via adding force on the z axis every FixedUpdate. Now this is working fine, but after I made a jump option, when the gameObject is in the air there are no drag, causing it to accelerate. The acceleration gets balanced out slowly after it hits the ground again, but if you keep spamming jump it will just get faster and faster.

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 Eno-Khaon · Oct 04, 2021 at 08:02 AM 0
Share

For reference...

 rb.AddForce(force * Time.fixedDeltaTime, ForceMode.VelocityChange)

... results in the same force as...

 rb.AddForce(force, ForceMode.Acceleration)

... on the basis that both of those ForceModes ignore mass, but Acceleration is already based around a constant force, where VelocityChange is based around a single impulse.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by SamyBoyJim · Oct 04, 2021 at 03:03 AM

I think the problem is friction. When your object is sliding along the ground, it is colliding with it and friction is acting upon it and when you jump up in the air, friction from the ground is not acting on your object. The best way to fix this is to add a physics material and remove the friction by setting it to 0. Then add the physics material to your rigidbody. This solution might cause your object to speed up because friction will no longer be acting on the object and you set these values while friction was active so you might want to change your speed values to something lower. Hope this helps!

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
0

Answer by Marscraft31415926 · Oct 04, 2021 at 05:43 AM

But then instead of adding force every FixedUpdate, I only need to do it once during void Start, right?

Edit: Well, that didn't go so well. By only adding one burst of speed, somehow with a physics material of 0 friction, and rigidbody of 0 drag on the player, it still slowed down.

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 Eno-Khaon · Oct 04, 2021 at 07:59 AM 0
Share

Something of note with respect to this: The default behavior of Physic Material interactions takes both colliding objects' friction values into consideration.

avatar image SamyBoyJim · Oct 05, 2021 at 04:27 AM 0
Share

You should still add the force every frame because other physics forces like air resistance are still acting on the object and will slow it down.

avatar image Eno-Khaon SamyBoyJim · Oct 05, 2021 at 04:41 AM 0
Share

Air resistance is the drag value, and it's calculated every physics cycle using something like:

 rb.velocity = rb.velocity * (1.0f - (rb.drag / Time.fixedDeltaTime));


It's a pretty lousy implementation, but that's PhysX's fault... Yes, if you have the default fixed time step of 50 physics cycles per second, a drag value of 50 will stop the object's movement every frame... And yes, it *IS* dependent on the Time.fixedDeltaTime value.

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

167 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

Related Questions

How to push a player in a certain direction? 2 Answers

How to move Enemy Gameobject with Rigidbody force/velocity to follow Player? 0 Answers

Mouse Follow Controller 2 Answers

Do rigid bodies with force add force to other objects? 0 Answers

3d character gets "sucked" and stuck on cube 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