• 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 logty · Aug 28, 2011 at 03:57 PM · rotationgravityplanet

3d gravity on a planet

I have this really annoying problem which I cannot seem to get to work, I am trying to make a planet, and have it so that my rigidbody character always has downwards gravity (already done) and is rotatating so that he always has the bottom side down (this seems to be the had part), any ideas? Thanks!

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 logty · Aug 28, 2011 at 05:30 PM 0
Share

Sorry, I need to clarify a little bit (this might make it a little more complicated) $$anonymous$$y planets are cubes, and I have the gravity working so if you are on one face then the gravity is down on that face etc., I just cant figure out how to get the rotation to work

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by syclamoth · Sep 14, 2011 at 11:19 AM

Well, if your planets are cubes then it's much easier to fake it. Just get the normal of the face you are currently on, and apply gravity in that direction! As for the rotation issue, try defining the axis of rotation and doing all your character rotations in terms of a rotation axis which is stored on the player. Every frame, interpolate it towards the current gravity direction, and that way the character will always stand up against their current "down". To find out which is the closed floor, shoot out raycasts in all directions every few frames, and whichever one is the shortest is deemed to be the new "down", and the gravity direction is set to the normal of the hit. This way, the character will always fall towards the nearest surface.

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 aldonaletto · Aug 28, 2011 at 04:31 PM

I created a rigidbody character which uses a local gravity that always points down to the surface it's on. The character adjusts its position to be always perpendicular to the ground. Take a look at it in this answer - the question is Walking on walls, but the character can walk over spheric surfaces as well.

EDITED: My script really doesn't work on a square planet - and may have problems in a spheric planet too, when the player is around the "south pole". I'm studying this case and will be back as soon as I have some conclusion (your case is really complicated!)

Comment
Add comment · Show 2 · 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 kidrockkenny · Jan 13, 2012 at 11:41 PM 0
Share

aldonaletto can you plz send me a copy of you gravity script its exactly what i need to add to my game i made a exact replica of google earth and that script would help me alot i just need it to keep me on the surface i have two spheres one is 60. by 60. which is the ground and the other thats on the outside of it is 80. 80. and it has no collision and its the atmospher plz help me out as soon as possible

avatar image aldonaletto · Jan 14, 2012 at 05:01 AM 0
Share

The script is in the question http://answers.unity3d.com/questions/155907/basic-movement-walking-on-walls.html

But there's a problem: when in the "south pole", the character becomes crazy and turns 180 degrees around its local Y axis without any apparent reason. It's a problem with quaternions, and I could not find a solution yet.

avatar image
0

Answer by ckfinite · Aug 28, 2011 at 04:44 PM

Try this (C#):

 using UnityEngine;
 using System.Collections;
 
 public class GravityScript : MonoBehaviour {
     public Transform gravitySource;
     public float gravScalar = 4.0f;
     public Vector3 rotOffset = Vector3.zero;
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void FixedUpdate () {
         var direction = gravitySource.position - transform.position;
         rigidbody.AddForce(direction.normalized * gravScalar);
         transform.rotation = Quaternion.LookRotation(direction.normalized) 
                                     * Quaternion.Euler(rotOffset);
     }
 }

Just set up rotOffset so that the model looks like it should, and it should work. If you want a exponential gravity model, that is also easily possible with this.

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 logty · Sep 14, 2011 at 11:08 AM 0
Share

Thanks for the script! The only problem is the rotation doesn't work, any ideas why?

avatar image logty · Sep 14, 2011 at 11:11 AM 0
Share

how should i set up RotOffset?

avatar image syclamoth · Sep 14, 2011 at 11:14 AM 0
Share

A bit of nitpicking- you should use Force$$anonymous$$ode.Acceleration in your rigidbody.AddForce function- this way it will be more gravity-like! Also you could attenuate gravity by the distance from gravitySource, so that the gravity becomes weaker as you get further and further away.

avatar image aldonaletto · Sep 14, 2011 at 02:30 PM 0
Share

Directing the gravity to the planet center creates a problem in a cubic planet: the character is attracted to the middle of the current face, because the gravity pulls it back when trying to go to the borders (thanks God Earth is not cubic!)

avatar image Tasarran · Jan 14, 2012 at 06:37 AM 0
Share

Yes, because the center of the cubic face is closer to the center of the cube than the edges or corners, so the face center would always be 'downhill'...

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Gravitational Rotation 4 Answers

Why is Quaternion.FromToRotation() not working properly? 3 Answers

Rotational Gravity - Camera and Force problems 1 Answer

bullet rotation as its affected by gravity 2 Answers

AddTorque but don't rotate around centre of mass? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges