Add Force if GameObject is detected in a Polygon Collider 2D

Let’s say that I have a Player, and my player can walk, run, and jump. Also, my player has a Polygon Collider 2D and if 2D GameObjects( with a Rigidbody2D and Box Collider 2D) are detected in the Polygon Collider 2D the Player will Add Force to movable 2D GameObjects that are in the Polygon Collider 2D to be pulled towards the Player.

(One)- How can I detect if any 2D GameObjects are in the Polygon Collider? & (Two)- How can I Add Force to movable 2D GameObjects that are in the Polygon Collider 2D to be pulled towards the Player?
------ Please code and explain your answer, for I am new to unity and won’t understand if you just give me directions, I don’t just use your code and forget about it, I write it down in my note book line to line and review and study it on my free time

Hello

To detect triggers follow these steps:

Prepare:

1-assign to both objects colliders (of any shape)

2-one of the colliders must have trigger option checked. (you’ll find it in the inspector) for now assign this to the movable object

3-at least one of the objects has to have rigid body checked on. (your player probably needs the rigidbody and already has it)

4- assign the main object (player in your case) a tag “Player for example”

5-Create a new c# script and put it on the movable object

Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class YourScriptName : MonoBehaviour {

	// Use this for initialization
	void Start () {
	}
	
	// Update is called once per frame
	void Update () {
	
	}


    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.tag == "Player")
            //Do something
    }




}

That is all for the first question.

this is the main thing to focus on

  void OnTriggerEnter2D(Collider2D other)
        {
            if (other.tag == "Player")
                //Do something
        }

it know that something entered in the trigger , then it checks what this thing is . Is it an object with the tag player ? if yes ->> do code , if no ignore everything.

Now for your second question which is adding force . it is simple , it should go in that //do something

  void OnTriggerEnter2D(Collider2D other)
        {
            if (other.tag == "Player")
                //Add Force
        }

To add force you need a rigidbody so go to the movable object and add a rigidbody (if not already done)

second step you need to create a rigidbody variable and assign it like the following :

public class YourScriptName : MonoBehaviour {
private Rigidbody2D Rigid;
void Start () {

        Rigid = GetComponent<Rigidbody2D>();
	}

Next step is to use the following line of code which takes care of adding a force:

 Rigid.AddForce(new Vector2(Value, Value));

you can adjust the Value by any number that suits you for example :

 Rigid.AddForce(new Vector2(20, 25)); // moves in combination of x and y direction
 Rigid.AddForce(new Vector2(0, 25)); // moves in y direction
 Rigid.AddForce(new Vector2(20, 0)); // moves x  direction

To sum up your code will look something like this:

  using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class YourScriptName : MonoBehaviour {
       private Rigidbody2D Rigid;
       public float Value;
      void Start () {
    
            Rigid = GetComponent<Rigidbody2D>();
    	}
        void Update()
{
}

   void OnTriggerEnter2D(Collider2D other)
            {
                if (other.tag == "Player")
                    Rigid.AddForce(new Vector2(Value, Value));
            }