gravity simulation script

I guys, i have a problem with a gravity simulation script

this is my script:

using UnityEngine;
using System.Collections;

public class gravity : MonoBehaviour {
    public float gravitationalAcceleration;    // on surface
    public float gravityRadius;
    public bool useExternalMass;
    public float distanceVariation = 0;    // variation between the gravity on surface and the gravity at gravityRadius(i.e. 0,5 is half gravity). 0 is no change

    float gravityRadiusInUnit;
    float unitDistanceVariation;
    SphereCollider gravitySphere;

    Vector3 position;

    // Use this for initialization
    void Start() {
        gravitySphere = gameObject.AddComponent<SphereCollider>();
        gravitySphere.isTrigger = true;
        gravitySphere.radius = gravityRadius;
        position = transform.position;
        gravityRadiusInUnit = gravitySphere.radius * transform.localScale.x;
        unitDistanceVariation = distanceVariation / ( gravityRadiusInUnit + transform.localScale.x );
        }

    // Update is called once per frame
    void Update() {
        position = transform.position;
        }

    void OnTriggerStay(Collider other) {
        if( other.attachedRigidbody ) {
            Vector3 otherPosition = other.transform.position;
            Vector3 direction = ( position - otherPosition ).normalized;
            float otherMass = 1;
            if( useExternalMass ) {
                otherMass = other.attachedRigidbody.mass;
                }
            other.attachedRigidbody.AddForce(direction * GravitationAccelerationAtPosition(otherPosition) * otherMass * Time.deltaTime, ForceMode.Acceleration);
            }
        }

    float GravitationAccelerationAtPosition(Vector3 otherPosition) {
        float distanceFromSurface/*  */ = ( otherPosition - position ).magnitude - ( transform.localScale.x / 2 );
        return ( 1 - ( unitDistanceVariation * distanceFromSurface ) ) * gravitationalAcceleration;
        }

    }

The parameters:
gravitationalAcceleration: the gravitational acceleration on the surface
gravityRadius: the radius of the gravity
useExternalMass: if true each gameobject react to gravity according to its mass
distanceVariation: variation between the gravity on surface and the gravity at gravityRadius(i.e. 0,5 is half gravity). 0 is no change

The script must be applied to a sphere.

The script creates a trigger sphere collider that applies a force to each gameobject inside it.

The script works correctly but after the “landing” the object start to “vibrate”, how can I fix this?

P.S.: Sorry for my poor english…

if its landed and AdForce is still active it will cause your problem…it will keep pushing

Yes, but if I disable the gravity force on the collision with the surface than the object can also stop itself in a strange position(i.e. a cube on a corner)

then use a circle collider to keep from getting stuck in a corner,but if you keep adding force its gonna keep pushing the cube no matter what…I know I had the same problem with a player hitting a saw blade…it will keep pushing him away as long as addForce is applied

With the circle collider I don’t get the desired effect

I think that the best way would be to stop the gravity force only if the vibrations are below a certain level, in this way the object movement would be real

I try to think a way to quantify the vibrations…

Is there a way to see how gravity is implemented in unity?

This is why most people just let the physics engine settle objects. This might also be mitigated better with acceleration rather than force.

It uses a constant. Look under edit.

I’m already using acceleration
The last parameter of addForce is ForceMode.Acceleration, is this what you mean?

I mean, can i see the code?

Right, missed that.

New to unity?
No, it’s closed source. So unless you are aiming to put something in orbit, I would highly suggest getting used to the physics that is already a part of the engine. Otherwise the solution is probably found in spring physics.

The problem is that I’m aiming to create something like Angry Birds Space and i would like to reuse the script for other similar project

Anyway, the problem is that after the contact with a planet every object begins to “shake” apparently caused by the force that continues to be applicated, i can stop this after a collision but in this way the object don’t behave correctly

But must there be a solution…