x


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); 
transform.LookAt(planet.transform.position); 
}

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

cidmodder gravatar image

cidmodder
221 145 110 126

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

Chris D gravatar image

Chris D
2.7k 13 13 36

also, for interest: http://www.gamasutra.com/view/feature/3593/gamesdemystifiedsupermario.php

Jun 20, 2011 at 10:18 PM Chris D

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

By RSS:

Answers

Answers and Comments

Topics:

x5830
x760

asked: Jun 20, 2011 at 09:57 PM

Seen: 2024 times

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