Counteract gravity
Im trying to get my character to float by counteracting gravity. My mass is 1 so this shuld work right?:
GetComponent<Rigidbody>().AddForce(new Vector3(0,9.81f,0));
This force seems way to high (even tho it seems right to me) since the caracter just takes of into the skies. At 3.5 im almost floating. I also tried this with the same results:
GetComponent<Rigidbody>().AddForce(-Physics.gravity);
So why is this not working? thanks in advance.
Answer by r3david · Aug 09, 2016 at 01:11 AM
I think your problem is related to using these lines of code in the Update function and not Fixed Update. If you are calling the code you showed in the FixedUpdate event, and it is still not working, you can try adding the velocity change in the Y component directly:
// use this outside of FixedUpdate for better performance
Rigidbody rb = GetComponent<Rigidbody>();
// counterbalance the velocity implied by the gravity
rb.velocity -= Physics.gravity*rb.gravityScale;
Answer by Rob2309 · Aug 09, 2016 at 12:39 AM
You can specify a ForceMode enum in the AddForce method. In your case I think you want to use ForceMode.Acceleration, as you want to apply an acceleration that is unaffected by the mass of your rigidbody, just like gravity is. You might also consider just using GetComponent().useGravity = false;
Your answer
Follow this Question
Related Questions
Script preventing Gravity from working 1 Answer
Unity3d gravity not working in movement script 0 Answers
How to add force in random directions? 0 Answers
Making an object drop when screen is touched 1 Answer
No Gravity Jump 0 Answers