0
\$\begingroup\$

a 2d planet in the center, where the players will be able to walk on it and also will be affected by center gravity. planet will be static and there won't be any other planets. so;

Is there a way that I can change the gravity position? yes you can adjust gravity via project settings but I want it to be in the middle x,y,z(0,0,0)

I have tried creating my own gravity but it wasn't very useful because objects wouldnt fall, stand on foot and also in contact with another object they would vibrate instead of standing still. (if they stand on the planet they would vibrate)

        private void FixedUpdate()
{
    foreach(GameObject item in grav)
    {
        Attact(item);
    }
}

private void Attact(GameObject gmf)
{
    Rigidbody2D rgg = gmf.GetComponent<Rigidbody2D>();

    Vector3 direction = rg2d.position - rgg.position;
    float distance = direction.sqrMagnitude;

    float forceMag = (10 * rgg.mass) / distance;
    Vector3 force = direction.normalized * forceMag * 10;

    rgg.AddForce(force);
}

I can create a script that will change the gravity by the opposite of the object's position. this does work but I cannot see any way to do it for more than one object.

\$\endgroup\$
  • 1
    \$\begingroup\$ Can you show us how you created your own gravity? We can't help you fix the bugs in your implementation if we can't see it. \$\endgroup\$ – DMGregory Oct 24 at 10:24
  • \$\begingroup\$ You can't use the Unity gravity system for point gravity sources. But you can very easily build your own gravity system. If you tried and failed, it would be useful to see your code so we can pinpoint your mistake. \$\endgroup\$ – Philipp Oct 24 at 10:33
  • \$\begingroup\$ Related: How can I accurately simulate orbits in Unity? Not really a duplicate because it's about 3d and also simulates gravity getting weaker with distance, but it might still be helpful. \$\endgroup\$ – Philipp Oct 24 at 10:34
1
\$\begingroup\$

Are you aware of the PointEffector2D component? I believe that is exactly what this is for.

See this tutorial (step 7). (Link directly to video)

\$\endgroup\$
  • \$\begingroup\$ One problem with the PointEffector2D component is that it adds a force and not an acceleration. The actual acceleration will be divided by the rigidbody mass. So when you have rigidbodies with different masses, they will experience different acceleration, which is not how gravity works in reality. \$\endgroup\$ – Philipp Oct 24 at 13:47
0
\$\begingroup\$

You can't use the Unity gravity system for point gravity sources. As mentioned by the answer by Ed Marty, you can use the PointEffector2D component to create a point gravity source, but only if all your rigidbodies have the same mass.

But you can also very easily build your own gravity system. Here is an example script for a "Gravity Source" component. Place it at your center of gravity, set the "gravity" value in the inspector to your desired acceleration and give it a trigger-collider which represents the zone in which it is supposed to affect objects.

using UnityEngine;

public class GravitySource : MonoBehaviour {

    public float gravity;

    void OnTriggerStay2D(Collider2D other) {
        if (other.attachedRigidbody) {
            Vector2 difference = this.gameObject.transform.position - other.gameObject.transform.position;
            Vector2 gravityDirection = difference.normalized;
            Vector2 gravityVector = gravityDirection * gravity;

            other.attachedRigidbody.AddForce(gravityVector, ForceMode.Acceleration);    
        }
    }
}

This script is a simplified version of my script from my answer to the question "How can I accurately simulate orbits in Unity?". The more complicated version works with 3d rigidbodies and simulates gravity getting weaker with distance.

What this script does not do is tell the objects it affects where "down" is. Depending on your game you might want to rotate objects towards the center of gravity under some conditions. If you want to add this, then you can use the gravityDirection vector for this purpose.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.