Rigidbodies passing through tilemap collider

EDIT: I found my trivial mistake, it was just making the sheep’s Rigidbody2D dynamic instead of kinematic

Hi all, this is my first post here, so sorry in advance if I mess things up.

As the title suggests, I’m having trouble with my objects (in this case, sheep) passing through my tilemap colliders (fences). I recorded what happens here:

As seen in the video, the player and a test object, “Mr Peanut”, collides with the fences, but the sheep goes right through.
I am using rigidbody2d.MovePosition in all three cases.
I suspect the issue comes from the script that moves the sheep. The script is added to the preyMovement object, and can be seen here:

    public float longRangeAttraction;
    public float predatorRepulsion;
    private GameObject playerObj = null;
    private void Start()
    {
      
        if (playerObj == null)
        {
            playerObj = GameObject.FindGameObjectWithTag("Player");
        }
    }


    private void FixedUpdate()
    {
        /* Movement
        Loop through each member and calculate their velocity, and from that update their position.
        Calculation requires looping through all members again, excluding the initial. */

        // Find all current prey and add them to the list preyList
        List<GameObject> preyList = new();
        foreach (GameObject fooObj in GameObject.FindGameObjectsWithTag("Prey"))  //Maybe find a way to only run this code if an object is removed
        {
            preyList.Add(fooObj);
        }
        // Loop through each prey in preyList and update its position by calculating its velocity
        foreach (GameObject prey in preyList)
        {
            Vector2 velocity = Move_prey(preyList, prey);

            // Align sprite direction with velocity
            SpriteRenderer spriteRenderer = prey.GetComponent<SpriteRenderer>();
            if (velocity.x < 0)
            {
                spriteRenderer.flipX = true;
            }
            else if (velocity.x > 0)
            {
                spriteRenderer.flipX = false;
            }

            // Movement
            Rigidbody2D preyRB = prey.GetComponent<Rigidbody2D>(); // Get j'th particle's rigidbody and update its position
            preyRB.MovePosition(preyRB.position + (velocity * Time.fixedDeltaTime));

        }

    }
    // The mathematical function that calculates the velocity for a prey
    private Vector2 Move_prey(List<GameObject> preyList, GameObject prey)
    {       int numberOfPrey = preyList.Count;
            Vector2 velocity = new Vector2(0, 0); // Velocity of j'th particle, will be updated
            foreach (GameObject otherPrey in preyList) // The k'th particle
            {
                if (otherPrey == prey)
                {
                    continue;
                }

                float dist = Vector2.Distance(prey.transform.position, otherPrey.transform.position);
                Vector2 velocityShortRange = (prey.transform.position - otherPrey.transform.position) / (dist * dist);
                Vector2 velocityLongRange = longRangeAttraction * (prey.transform.position - otherPrey.transform.position);

                velocity += (velocityShortRange - velocityLongRange) / numberOfPrey;

            }
            float dist_pred = Vector2.Distance(prey.transform.position, playerObj.transform.position);
            Vector2 velocityPredRepulsion = predatorRepulsion * (prey.transform.position - playerObj.transform.position) / (dist_pred * dist_pred);
            velocity += velocityPredRepulsion;
        return velocity;
    }

The “Mr. Peanut” object’s movement is simply:

       Vector2 velocity = new Vector2(1, 0);
        rb.MovePosition(rb.position + velocity * Time.fixedDeltaTime);

And the player’s movement is similar, just with inputs instead.

I also tried using physics.Raycast to check if there was an obstacle in front of the sheep, but it never detected any.

I hope my post meets the community standards.
Thanks in advance.

I feel like an absoule idiot, I used like 5 hours on this, just to find out it was literally just making the Rigidbody2D on the sheep “Dynamic” instead of “Kinematic”.

Well, lesson learned I guess:)

1 Like