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 NutellaDaddy · May 21, 2014 at 02:51 AM · c#physicsgravity

Planetary gravity for not so round object?

I tried to use the FauxGravity scripts ,but they only work on perfectly spherical planets. I am trying to override the unity 3d built in physics to create physics for the player to stand on an object like a planetoid. It is not perfectly circular so the script doesn't work. Does anybody have any scripts or tips on how I could get a good gravity system like that to work?

I could just create my own movement functions in the script that controls the gravity to create a whole new physics and movement system. What do you think. It would be easier to make the walking forward and everything.

Comment
Add comment
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

1 Reply

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

Answer by Clet_ · May 21, 2014 at 04:15 AM

Assuming you don't have corners on that not-so-round object, the way to achieve that would be by applying the gravity in the opposite direction of the normal of the surface you're walking on.

You'd have to store the normal every frame, to adapt to the planet's curve and then, raycast in that direction, updating the normal and apply gravity in the opposite direction of that new normal

 private Vector3 _lastNormal = Vector3.up; //This line assumes you're first frame normal is looking upward
 private Vector3 _velocity;
 public float gravity;
 
 
 private void FixedUpdate () {
     RaycastHit hit;
     if(Physics.Raycast (transform.position, -_lastNoraml, out hit, 10f, ~(1 << LayerMask.NameToLayer ("MyPlanetLayer"))) {
         _lastNormal = hit.normal;
     }
 
     _velocity += gravity * -_lastNormal * Time.deltaTime;
     _transform.position += _velocity * Time.deltaTime;
 }

I didn't test this code, but the idea of it is relatively obvious. Your turn to adapt it

Comment
Add comment · Show 4 · 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 Clet_ · May 21, 2014 at 04:25 AM 0
Share

mightysprite comment on $$anonymous$$ileSplit's answer invalidated this method. Please disregard

avatar image NutellaDaddy · May 21, 2014 at 04:53 AM 0
Share

Could I switch out the layer with the gameobject of the planet ins$$anonymous$$d?

avatar image Clet_ · May 21, 2014 at 11:56 PM 0
Share

The layer is simply there to avoid to raycast against every gameObject with a collider in your scene.

If you put your planet on the "$$anonymous$$yPlaneLayer", it will be a quicker raycast than a raycast against every kind of collider and then checking iof it's the planet.

The ~ is the binary NOT. Each number are written in binary, and this operator just changes every 1's into 0's and vice-versa. The line

 ~(1 << Layer$$anonymous$$ask.NameToLayer("$$anonymous$$yPlanetLayer"))

is there to make sure the collision test is made ONLY on the $$anonymous$$yPlanetLayer. Layer$$anonymous$$ask script reference

avatar image mightysprite · Jul 03, 2014 at 12:16 AM 0
Share

Since the answer to which I made a comment seems to have disappeared (and @Clet_ referenced it above), I suspect that my comment generally said that you may not find this method working quite like you expect. Since you'll always jump and fall perpendicular to the surface, when you're on a "hill" you'll jump also perpendicular to that surface. The steeper the hill from the "expected horizontal plane," the more awkward it will feel. What you could do though is some weighted blend between the normal of the polygon you're on and the direction from the center of mass to the player, this might feel ok but you'll have to play with it to see.

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

23 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

Related Questions

How could I simulate planetary gravity that has an orbit point? 1 Answer

How do I make a small planet with gravitational pull? 4 Answers

Pinball, Sphere and gravity, hitting sphere off the table 1 Answer

Constant force, with gravity, and correct collisions. 2 Answers

Planet gravity help need guidance no code 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