Hello! I'm trying to make a custom gravity script and I found one from cheatsguy777 on the forums that does apply gravity. I am getting my models to walk around a planet and the gravity works fine at the top pole. but at the bottom or at the side the model just falls off. I want it to stay normal to the planet all the time and have the gravity applied in the down direction but it doesn't become normal ever. I do have rigidbodies on everything and colliders and gravity is turned off on all rigid bodies. how could i change this script to make it work?
var planet : GameObject;
var gravity = 2;
rigidbody.velocity.x = ((-transform.position.x + planet.transform.position.x)*gravity);
rigidbody.velocity.y = ((-transform.position.y + planet.transform.position.y)*gravity);
rigidbody.velocity.z = ((-transform.position.z + planet.transform.position.z)*gravity);
I'm putting the script on the player walking around the planet. I've tried putting it on just the planet but that didn't seem to work either. Thanks for the help!
Do you have Unity's gravity being applied to your character also? Does the character stay normal and just fall off or does staying normal not work either?
gravity has been turned off although everything does have a rigidbody. it doesn't stay normal either
Get the normal of the surface from the collision generated upon the surface of the "planet." Then base the gravitational force on the inverse of that normal and rotate your model accordingly.
Answer by Chris D
Jun 20, 2011 at 10:17 PM
I suggest looking through previous questions asking about relative gravity or Mario Galaxy type mechanics. I answered a similar question a while ago, it'd probably apply to you, too.
also, for interest: http://www.gamasutra.com/view/feature/3593/games_demystified_super_mario_.php
after looking through alot of that it seems like theres a problem with the character controller. why is that and how does it effect doing raycasts? I could do it with an object but only if there was no controller. That leaves me wondering how i'm going to control the character.
Have you given it a shot with the controller and it's still bugging out?
if i do it with any of the controllers it won't rotate the body to be normal to the surface. if i just use the capsule and camera with no controllers then it will turn to be normal to the sphere all the time.
my guess (I haven't looked at the controller code) is that there's a line in the code that's resetting the controller's orientation every frame or locking it altogether...
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
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, check out our FAQ for more information.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
4 People are following this question.
Zero gravity movement
Character teleports to the apex of full jump height
Rigidbody not responding to setting gravity, but setting in inspector works.
Setting Scroll View Width GUILayout