Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved Rigidbodies passing through tilemap collider

Discussion in 'Physics' started by TwoBeAss, Aug 23, 2022.

  1. TwoBeAss

    TwoBeAss

    Joined:
    Aug 20, 2022
    Posts:
    2
    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:

    Code (CSharp):
    1.     public float longRangeAttraction;
    2.     public float predatorRepulsion;
    3.     private GameObject playerObj = null;
    4.     private void Start()
    5.     {
    6.      
    7.         if (playerObj == null)
    8.         {
    9.             playerObj = GameObject.FindGameObjectWithTag("Player");
    10.         }
    11.     }
    12.  
    13.  
    14.     private void FixedUpdate()
    15.     {
    16.         /* Movement
    17.         Loop through each member and calculate their velocity, and from that update their position.
    18.         Calculation requires looping through all members again, excluding the initial. */
    19.  
    20.         // Find all current prey and add them to the list preyList
    21.         List<GameObject> preyList = new();
    22.         foreach (GameObject fooObj in GameObject.FindGameObjectsWithTag("Prey"))  //Maybe find a way to only run this code if an object is removed
    23.         {
    24.             preyList.Add(fooObj);
    25.         }
    26.         // Loop through each prey in preyList and update its position by calculating its velocity
    27.         foreach (GameObject prey in preyList)
    28.         {
    29.             Vector2 velocity = Move_prey(preyList, prey);
    30.  
    31.             // Align sprite direction with velocity
    32.             SpriteRenderer spriteRenderer = prey.GetComponent<SpriteRenderer>();
    33.             if (velocity.x < 0)
    34.             {
    35.                 spriteRenderer.flipX = true;
    36.             }
    37.             else if (velocity.x > 0)
    38.             {
    39.                 spriteRenderer.flipX = false;
    40.             }
    41.  
    42.             // Movement
    43.             Rigidbody2D preyRB = prey.GetComponent<Rigidbody2D>(); // Get j'th particle's rigidbody and update its position
    44.             preyRB.MovePosition(preyRB.position + (velocity * Time.fixedDeltaTime));
    45.  
    46.         }
    47.  
    48.     }
    49.     // The mathematical function that calculates the velocity for a prey
    50.     private Vector2 Move_prey(List<GameObject> preyList, GameObject prey)
    51.     {       int numberOfPrey = preyList.Count;
    52.             Vector2 velocity = new Vector2(0, 0); // Velocity of j'th particle, will be updated
    53.             foreach (GameObject otherPrey in preyList) // The k'th particle
    54.             {
    55.                 if (otherPrey == prey)
    56.                 {
    57.                     continue;
    58.                 }
    59.  
    60.                 float dist = Vector2.Distance(prey.transform.position, otherPrey.transform.position);
    61.                 Vector2 velocityShortRange = (prey.transform.position - otherPrey.transform.position) / (dist * dist);
    62.                 Vector2 velocityLongRange = longRangeAttraction * (prey.transform.position - otherPrey.transform.position);
    63.  
    64.                 velocity += (velocityShortRange - velocityLongRange) / numberOfPrey;
    65.  
    66.             }
    67.             float dist_pred = Vector2.Distance(prey.transform.position, playerObj.transform.position);
    68.             Vector2 velocityPredRepulsion = predatorRepulsion * (prey.transform.position - playerObj.transform.position) / (dist_pred * dist_pred);
    69.             velocity += velocityPredRepulsion;
    70.         return velocity;
    71.     }
    The "Mr. Peanut" object's movement is simply:
    Code (CSharp):
    1.        Vector2 velocity = new Vector2(1, 0);
    2.         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.
     
    Last edited: Aug 23, 2022
  2. TwoBeAss

    TwoBeAss

    Joined:
    Aug 20, 2022
    Posts:
    2
    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:)
     
    Drakkith likes this.