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 /
avatar image
2
Question by RetepTrun · Jul 07, 2011 at 10:47 PM · physicsrigidbodygravityforcespace

Artificial Gravity

I intend to make a game that takes place in space where one can land a craft on a larger "carrier" type ship, walk around on it, or even drive a little car about the decks. I would like to make a more complete artificial gravity system than just turning on "use gravity" for objects within the ship.

The method I am trying to use is to apply a downwards force on any object within a trigger collider. This way, if the carrier does a roll, none of the cargo will fall off into space.

 function OnTriggerStay (other : Collider) {    
 
     if (other.attachedRigidbody) {
         other.attachedRigidbody.AddForce(0,other.attachedRigidbody.mass*-9.81,0);
     }
 }

the problem is that whether I use "AddForce" or "AddRelativeForce" things on the ship dont seem to behave realistically.

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 Dreamblur · Jul 07, 2011 at 10:57 PM 0
Share

Just attach a ConstantForce component on your objects, set the relative force on those to whatever you want them to be, and rotate the objects based on the normal of the surface that they are currently resting on.

2 Replies

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

Answer by RetepTrun · Jul 18, 2011 at 08:04 PM

If the ship is permanently fixed upright then gravitytype can be used, otherwise use forcetype where landing on a ship which is upside down you wont end up falling to the ceiling

 var forcetype:boolean;
 var gravitytype:boolean;
 
 function OnTriggerStay(other : Collider) {
 
  if(forcetype){
         if (other.attachedRigidbody) {
     
         
     
             // Calculate the y-axis relative to us    
             var cameraRelativeRight : Vector3 = transform.TransformDirection (Vector3.up);
             // Apply a force relative to the our y-axis
             
         other.attachedRigidbody.AddForce(cameraRelativeRight * -9.81,ForceMode.Acceleration);
         }
     }
     
     
 } 
 
 
 
 //works if upright
 
 function OnTriggerEnter(other : Collider) {
     if(gravitytype){
         
         if(other.attachedRigidbody) {
                 other.attachedRigidbody.useGravity = true;
             
         }
     }
 }
 
 function OnTriggerExit(other : Collider){
     if(gravitytype){
         if (other.attachedRigidbody) {
             other.attachedRigidbody.useGravity = false;
         }
     }
 }
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
3

Answer by SilverTabby · Jul 07, 2011 at 10:57 PM

Use

 var gravityVector = Vector3(0, -9.81, 0);
 rigidbody.AddForce( gravityVector * Time.deltaTime, ForceMode.Acceleration);

The main diffrences are that this does not take into account the mass of the object - just like gravity doesn't - and the ForceMode.Acceleration will make it behave more like gravity because gravity is an acceleration, not a constant force.

Also putting gravity in it's own vector makes it easier to modify

gravityVector = Vector3.up * 9.81; // :)

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 royce3 · Jan 18, 2012 at 02:01 PM 0
Share

you want to use mothership.transform.up, not Vector3.up

avatar image sdgd · Aug 15, 2013 at 03:35 PM 0
Share

thanks @SilverTabby exactly what I was looking for

I need for planetary gravity so if I go to the other site I don't end up falling from the planet :P

now I just have to figure out how to turn my body around the sphere, ...

avatar image ruudlenders · Jul 02, 2014 at 04:28 PM 0
Share

You might want to place this in FixedUpdate(), and if you do, remove Time.deltaTime.

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

7 People are following this question.

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

Related Questions

Player in Spaceship 2 Answers

What's the Rigidbody's gravity unit? 1 Answer

Problems making a glider 3 Answers

Realistic/No gravity spaceship movement? 1 Answer

Gravitational Object Creation for 3D Game 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