I need some help with physics problem C#

I have a game with some moving rectangles. They move around at random in a set space and when they collide with something they stop moving. The problem I have though is after they collide with something they sometimes move in that same direction again but since they already collided and haven’t left each other then they don’t get called to stop and they just push at each other forever. I know I could use OnCollisionStay but that seems pretty resource heavy especially if I have 10 to 20 enemies moving around at the same time.

just change the rigidbody to iskinematic on collision

use is kinetic ticked

When I do that they push each other out of the set range and thru walls. Ill post my current code:

using UnityEngine;
using System.Collections;


public class AIMovement : MonoBehaviour
{
    public float minMoveDelay = 3f;
    public float maxMoveDelay = 6f;
    public float speed = 2f;
    public AddOptions extraOptions;
    // private vars
    //The area the enemy can move in
    private float minX = 4.4f;
    private float maxX = -4.5f;
    private float minY = 4.5f;
    private float maxY = -2.5f;
    private Vector2 newPos;
    private bool moving = true;
    private Vector3 startPos;
    // Use this for initialization
    void Start ()
    {
        FindPlace();
        startPos = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z);
    }
   
    // Update is called once per frame
    void Update ()
    {
        if(moving == true && extraOptions.doesMove == true)
        {
            Move();
        }
        if(extraOptions.avoidBall == true)
        {
            AvoidBall();
        }
    }
   
    void AvoidBall()
    {
        // Slowly moves away from the ball   
    }

    void FindPlace()
    {
        // Search for position to move to
        newPos.Set(Random.Range(minX, maxX), Random.Range(minY, maxY));
    }
   
    void Move()
    {
        Vector3 temp = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z);
        // Moves the brick in the direction of the transform
        if(extraOptions.lockXAxis == false)
        {
            if(gameObject.transform.localPosition.x < newPos.x)
            {
                temp = new Vector3(gameObject.transform.position.x + (speed/100), gameObject.transform.position.y, gameObject.transform.position.z);
                gameObject.transform.position = temp;
            }
            if(gameObject.transform.localPosition.x > newPos.x)
            {
                temp = new Vector3(gameObject.transform.position.x - (speed/100), gameObject.transform.position.y, gameObject.transform.position.z);
                gameObject.transform.position = temp;
            }
        }
        else
        {
            startPos = new Vector3(startPos.x, gameObject.transform.position.y, gameObject.transform.position.z);
        }
        if(extraOptions.lockYAxis == false)
        {
            if(gameObject.transform.localPosition.y < newPos.y)
            {
                temp = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y + (speed/100), gameObject.transform.position.z);
                gameObject.transform.position = temp;
            }
            if(gameObject.transform.localPosition.y > newPos.y)
            {
                temp = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y + (speed/100), gameObject.transform.position.z);
                gameObject.transform.position = temp;
            }
        }
        else
        {
            startPos = new Vector3(gameObject.transform.position.x, startPos.y, gameObject.transform.position.z);
        }
        if(gameObject.transform.localPosition.x <= newPos.x + .25 && gameObject.transform.localPosition.x >= newPos.x - .25 || extraOptions.lockXAxis == true)
        {
            if(gameObject.transform.localPosition.y <= newPos.y + .25 && gameObject.transform.localPosition.y >= newPos.y - .25 || extraOptions.lockYAxis == true)
            {
                if(extraOptions.lockXAxis == false || extraOptions.lockYAxis == false)
                {
                    Stopped();
                }
            }
        }
    }
   
    void Stopped()
    {
        // Arived at target or hit something that stopped it
        StartCoroutine(MovementDelay());
    }
   
    IEnumerator MovementDelay()
    {
        // Delays the movement for X ammount of time
        moving = false;
        FindPlace();
        yield return new WaitForSeconds(Random.Range(minMoveDelay, maxMoveDelay));
        moving = true;
    }
   
    void OnCollisionEnter2D(Collision2D coll)
    {
        if(coll.gameObject.tag != "Ball" || extraOptions.stopIfHit == true)
        {
            Stopped();
        }
    }
    [System.Serializable]
    public class AddOptions
    {
        public bool lockYAxis = false;
        public bool lockXAxis = false;
        public bool avoidBall = false;
        public bool doesMove  = true;
        public bool stopIfHit = true;
    }
}

check the tag for gm ball

I don’t think you under stood what i was saying. I need the objects to know when they are touching another object not just when they colide.

you may use contact point

on OnCollisionEnter

I see use contactPoint to get the collider and then compare where each other is at in world space and move the opposite way. Thanks :slight_smile:

u r welcome