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 /
avatar image
0
Question by Azound · Jan 20, 2012 at 07:21 AM · physicsrigidbodygravitysleep

How do I change force of gravity for a single object?

I've got an object that I want to fall at an acceleration of 3 G's. My current method of doing this is to turn gravity off for the object, and apply a downward force of 3*Physics.gravity each tick. Unfortunately, applying a force every tick prevents the rigidbody from ever sleeping.

My hope is to have a lot of these objects, have them fall, and once they reach a resting place, put the rigidbody to sleep. This all works as expected if I let the rigidbody use its built-in gravity, but then I can't modify the gravity force for individual objects.

Anybody know how to solve this?

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 Fattie · Jan 20, 2012 at 08:32 AM 0
Share

Dear Azound, "Gs" is just a plural -- there is no apostrophe!

Perhaps you could hit "edit" and get rid of that apostrophe! Cheers

4 Replies

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

Answer by aldonaletto · Jan 20, 2012 at 10:40 PM

You can apply a force as you were already doing, but check rigidbody.velocity: when it's below a low value during some number of consecutive physics cycles, remove the force and let the rigidbody sleep like a log:

 private var limit: float = 0.01;
 private var cycles: int;
 private var cyclesToSleep: int = 10;
 
 function FixedUpdate () {
     if (rigidbody.velocity.sqrMagnitude > limit){
         cycles = cyclesToSleep; // reload counter if velocity isn't negligible
     }
     if (cycles > 0){ // while counter > 0 apply force
         cycles--;
         rigidbody.AddForce(3 * Physics.gravity);
     }
 }

If something hits and moves the rigidbody, the force is re-enabled.

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 Azound · Jan 21, 2012 at 12:19 AM 1
Share

I'm not doing the force application inside a behaviour - It's in a callback that has access to the gameObject, but doesn't have any place to store a cycles count. I could reengineer everything to work inside an object that has state, and I may have to if the ConstantForce technique doesn't work. It would be easy to check the velocity, but without watching it over time, you can't be sure whether it just reached the apex of a toss straight up, or settled on the ground.

avatar image aldonaletto · Jan 21, 2012 at 01:19 AM 0
Share

I tried ConstantForce, but it didn't let the rigidbody sleep. $$anonymous$$aybe you could keep the original gravity and add the force only when the velocity isn't negligible: the extra gravity would be turned off when reaching an apex, but the original one would keep moving the rigidbody, turning the extra gravity on again quickly enough to avoid any noticeable glitch.

avatar image Azound · Jan 21, 2012 at 05:52 AM 0
Share

Aldonaletto's variation is the one I went with. ConstantForce does prevent sleeping, so I created my own behaviour and count cycles where velocity is low.

Obviously, you may want to change my Update function to FixedUpdate, but here's my class:

using UnityEngine;

[RequireComponent(typeof(Rigidbody))] class ApplyForce : $$anonymous$$onoBehaviour { public Vector3 force = Vector3.zero; public Vector3 torque = Vector3.zero;

   /// <summary>
   /// Causes the force to be applied in the gameObject's local space.
   /// </summary>
   public bool relative = false;
   /// <summary>
   /// Causes the force to ignore mass and accelerate at the given force rate.
   /// </summary>
   public bool accelerate = false;
   
   public float sleepVelocity = 0.01f;
   
   private int cycles = 0;
 
   public void Update() {    
      if (rigidbody.velocity.sqr$$anonymous$$agnitude < sleepVelocity * sleepVelocity) {
         ++cycles;    
      }
      else {
         cycles = 0;
      }
          
      if (cycles > 3) {
         Object.Destroy(this);
         return;
      }
          
      Force$$anonymous$$ode mode = accelerate ? Force$$anonymous$$ode.Acceleration : Force$$anonymous$$ode.Force;
      if (relative) {
         rigidbody.AddRelativeForce(force, mode);
         rigidbody.AddRelativeTorque(torque, mode);
      }
      else {
         rigidbody.AddForce(force, mode);
         rigidbody.AddTorque(torque, mode);
      }
   }

}

avatar image
0

Answer by dannyskim · Jan 20, 2012 at 08:21 AM

You could turn gravity off and apply a constant force, and then turn that constant force off or manipulate the constant force to your specifications. You can add a constant force a rigidbody by going to component -> physics -> constant force. Or you can change the constant force by script if you wish:

http://unity3d.com/support/documentation/ScriptReference/ConstantForce.html

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 Azound · Jan 20, 2012 at 06:55 PM 0
Share

I forgot that ConstantForce was a built-in type. I'll give that a try tonight and see if it plays nicely with rigidbody sleeping :)

avatar image
0

Answer by Kryptos · Jan 20, 2012 at 08:28 AM

You can try to apply by default a very strong gravity (3G's) but put a big drag on your rigidbodies to make them look as they were falling at only one G.

Then if you need an object to fall at 3G's, just put the drag to zero (or a very small value).

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 Kryptos · Jan 20, 2012 at 08:29 AM 0
Share

Or maybe mixing my solution with the one suggested by @dannyskim.

avatar image Azound · Jan 20, 2012 at 06:56 PM 0
Share

Ha! gotta love a good hack :) Hopefully the constant force technique works, but I'll keep this in $$anonymous$$d as a backup.

avatar image MidgardSerpent · Jan 21, 2012 at 01:30 AM 0
Share

The drag suggestion is one I would have made as well....

avatar image
0

Answer by Creator347 · Jul 02, 2013 at 06:58 AM

Physics.gravity = new Vector3(0, -1.0F, 0);

http://docs.unity3d.com/Documentation/ScriptReference/Physics-gravity.html

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

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Movement on a tube 1 Answer

Rigidbody ball physics 1 Answer

Turning gravity on makes objects fly 2 Answers

[Complex] How to move the player similar to character controller while also applying rigidbody physics vectors 1 Answer

How to disable this "friction"? 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