x


How do I make a small planet with gravitational pull?

I have a rocket ship flying into space in a 2D (3D art) side-scrolling game. My question is, assuming the gravity within the project settings is necessary to the gameplay, how do I create isolated gravitational fields with limited range?

For example, if I'm flying my rocket against the gravity pulling down, I also want to encounter planetoids that pull me in if I get too close. It must be created within the physics system because the rocket needs to be able to resist it (or even land on the planetoid).

Thank you for any answers sent my way!

more ▼

asked Mar 22 '10 at 10:14 PM

James gravatar image

James
45 3 3 8

(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

Thanks for the help everybody, here's how I ended up solving the problem.

function Update () 
{

}

function OnTriggerStay (other : Collider)
{
    other.attachedRigidbody.useGravity = false;

    var direction = -(other.attachedRigidbody.transform.position - transform.position);

    if (other.attachedRigidbody)
    {
        other.attachedRigidbody.constantForce.force = direction;
    }
}

function OnTriggerExit (other : Collider)
{
    other.attachedRigidbody.useGravity = true;
    other.attachedRigidbody.constantForce.force = Vector3.zero;
}
more ▼

answered Mar 23 '10 at 07:46 PM

James gravatar image

James
45 3 3 8

(comments are locked)
10|3000 characters needed characters left

I saw your post and think this is worth taking a look at on the Unify Wiki page. It has a Simulation section on the scripts page.

Quote from page: "Gravity.js- Provides a relatively accurate simulation of body-to-body gravity (i.e. plantary gravity)."

Here is the link: http://www.unifycommunity.com/wiki/index.php?title=Gravity

I'm new too, hope it helps.

more ▼

answered Mar 23 '10 at 05:34 AM

DigitalDogfight gravatar image

DigitalDogfight
137 8 10 16

(comments are locked)
10|3000 characters needed characters left

I am no expert by any means, but my first thought would be to have a sphere with sphere collider that would represent the reach of the gravitational pull. set the collider to be a trigger. Then you would need a script on the object that is going to be acted upon by the gravity that would register that it is in range to be under the gravitational pull and then calculate the distance from the planet to work out how much gravity should be applied. This is where I would start, but that doesn't necessarily mean that it would be the best way.

more ▼

answered Mar 22 '10 at 10:19 PM

n8 gravatar image

n8
192 11 14 26

(comments are locked)
10|3000 characters needed characters left

To add to that, the way I would apply the gravity in the script would probably be to alter the rotation and transforms, so every second the ship should be moved some amount closer, or something along those lines.

more ▼

answered Mar 23 '10 at 03:34 AM

user-1794 gravatar image

user-1794
1 2 2 2

but that kind of works against the physics engine in unity. Adding forces to the rigidbody component is usually a better idea

Mar 23 '10 at 10:50 AM KvanteTore
(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:

x5098
x1881
x464

asked: Mar 22 '10 at 10:14 PM

Seen: 7581 times

Last Updated: Mar 22 '10 at 10:14 PM