Before updating to Windows 10 you may need to return your Pro license. More information here

Gravity script problem

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; 
 function FixedUpdate(){ 
 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!

more ▼

asked Jun 20, 2011 at 09:57 PM

avatar image

231 145 112 130

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?

Jun 20, 2011 at 10:06 PM Chris D

gravity has been turned off although everything does have a rigidbody. it doesn't stay normal either

Jun 20, 2011 at 10:08 PM cidmodder

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.

Jun 20, 2011 at 10:37 PM Dreamblur
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Jun 20, 2011 at 10:17 PM

avatar image

Chris D
2.8k 14 15 39

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.

Jun 21, 2011 at 02:09 AM cidmodder

Have you given it a shot with the controller and it's still bugging out?

Jun 21, 2011 at 02:15 AM Chris D

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.

Jun 21, 2011 at 07:07 PM cidmodder

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...

Jun 21, 2011 at 08:24 PM Chris D
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Jun 20, 2011 at 09:57 PM

Seen: 2174 times

Last Updated: Jun 24, 2011 at 06:38 PM