Can anyone tell me what I'm doing wrong here?

Hi all, I’m trying to create a planetary body type script to have objects fall in and out of orbit based on their original velocity multiplied by the force of gravity emitted by the planet in the direction of the planet.

I’ve tried what I thought would have worked, but for some reason absolutely nothing is happening to my objects (the script is attached to them).

They are prefabs that are instantiated at random locations but when they collide with the gravity trigger I need them to have force added to them in the direction of the planet.

Here is what I’ve got so-far:

    public float gravityAmount = 100;
    float moveSpeed;

    Rigidbody2D rb;

    public GameObject ball;

    bool insideGravity;

    void Awake()
    {
        ball = GameObject.FindGameObjectWithTag("Damager");
    }

    void Start()
    {
        insideGravity = false;

        rb = GetComponent<Rigidbody2D>();
    }

    void Update()
    {

        ball = GameObject.FindGameObjectWithTag("Damager");

        if (insideGravity)
        {
            //This line is the trouble I'm pretty sure.
            rb.AddForce((ball.transform.position - transform.position) * gravityAmount * Time.smoothDeltaTime);
        }

        Vector2 movement = new Vector2(xDirection, yDirection).normalized;

        rb.velocity = movement * moveSpeed;
    }

	void OnTriggerEnter2D(Collider2D col)
    {
        if (col.gameObject.tag == "Magnet")
        {
            insideGravity = true;
        }
    }
}

I also thought it could have been using both AddForce and velocity at the same time so I tried using this line instead:

rb.velocity = ball.transform.position - transform.position * gravityAmount * Time.smoothDeltaTime;

But still no change. I’d really appreciate any help you can give me, thanks!

There are a few things that could be wrong, even outside that code. I know some stuff look like too basic, but check anyway:

  • Make sure the script is enabled when the prefab enters the magnet area
  • Make sure that both the object with the script and the planet have colliders enabled
  • Make sure the planet’s collider is marked as trigger
  • At least one of the objects must also have a RigidBody2D (it looks like you have that covered)
  • Check the layers of the planets and the prefab and make sure their collision is checked in the collision matrix: Unity - Manual: Layer-based collision detection
  • Make sure that OnTriggerEnter2D is being called, and, more important, that the line “insideGravity = true;” is being executed. Just write something to the console before that line
  • Make sure xDirection and yDirection (where are those defined??) are not 0
  • Make sure moveSpeed is not 0
  • Make sure there are no errors or warnings in the console
  • Make sure the Damager and Magnet tags are correctly set in the objects

Also, doing physic stuff in the Update function is not recomended, even if you use smoothDeltaTime. You should change physic values in FixedUpdate if you want to get consistent results independant of the FPS of your game.