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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

BoxCollider2D collisions not working for some reason

Discussion in 'Physics' started by Caelorum, Apr 29, 2015.

  1. Caelorum

    Caelorum

    Joined:
    Feb 6, 2014
    Posts:
    32
    I have two objects. One is spawned with a bouncy physics material, and it's velocity is set towards the other object. They should collide, one should bounce off. However they are just passing through each other most of the time.

    -I've set both rigidbodies to continuous collision.
    -I've set a script that prints "collided" when the two collide, so it's saying they are colliding.
    -However nothing is happening.

    Sometimes they will collide, sloppily, but mostly they just pass through and collide with the polygon collider behind the other object.

    Here is a picture example



    Here is the script for thing that fires the smaller square

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BasicEnemy : MonoBehaviour
    5. {
    6.     public GameObject bullet;
    7.     public float bulletSpeed = 20;
    8.  
    9.     bool inFireArea = false;
    10.     bool fireTime = true;
    11.     GameObject player;
    12.  
    13.     void Awake()
    14.     {
    15.         player = GameObject.FindGameObjectWithTag ("Player");
    16.     }
    17.  
    18.     void Update()
    19.     {
    20.         CheckFireArea();
    21.         if(inFireArea == true && fireTime == true)
    22.         {
    23.             fireTime = false;
    24.             Fire ();
    25.         }
    26.         LookAtPlayer ();
    27.     }
    28.  
    29.     void LookAtPlayer()
    30.     {
    31.         Vector3 dir = player.transform.position - transform.position;
    32.         float angle = Mathf.Atan2(dir.y,dir.x) * Mathf.Rad2Deg;
    33.         transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
    34.     }
    35.  
    36.     void CheckFireArea()
    37.     {
    38.         Vector2 playerPos = player.transform.position;
    39.         Vector2 pos = transform.position;
    40.         float distance = pos.x - playerPos.x;
    41.         if(distance < 40 && distance >= -1)
    42.         {
    43.             inFireArea = true;
    44.         }
    45.         else
    46.         {
    47.             inFireArea = false;
    48.         }
    49.     }
    50.  
    51.     void Fire()
    52.     {
    53.         GameObject bulletZone = this.transform.Find ("FireFrom").gameObject;
    54.         Vector3 facing = this.transform.right;
    55.         facing.x = facing.x + .6f;
    56.         GameObject bulletInstance = Instantiate (bullet, bulletZone.transform.position, Quaternion.identity) as GameObject;
    57.         Rigidbody2D rb = bulletInstance.GetComponent<Rigidbody2D>();
    58.         rb.velocity = facing * bulletSpeed;
    59.         StartCoroutine ("ResetFireTime");
    60.     }
    61.  
    62.     IEnumerator ResetFireTime()
    63.     {
    64.         yield return new WaitForSeconds(.7f);
    65.         fireTime = true;
    66.     }
    67. }
    Here is the script for the rectangle it is supposed to collide with

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Shield : MonoBehaviour
    5. {
    6.     GameObject shieldBase;
    7.     Transform rotation;
    8.     Vector2 shieldPos;
    9.     void Awake()
    10.     {
    11.         shieldBase = GameObject.FindGameObjectWithTag ("Follow");
    12.     }
    13.     void Update()
    14.     {
    15.         ShieldMovement ();
    16.     }
    17.  
    18.     void ShieldMovement()
    19.     {
    20.         rotation = shieldBase.transform;
    21.         shieldPos = shieldBase.transform.position;
    22.         transform.rotation = rotation.rotation;
    23.         transform.position = shieldPos;
    24.     }
    25.  
    26. }
    There should be no conflicts between either script. So i don't see why the objects wouldn't collide.
    What's really weird is i've literally done this prototype before with no collision issues. I don't understand why it wouldn't be working now.
     
  2. inaworldofideas

    inaworldofideas

    Joined:
    Sep 4, 2013
    Posts:
    7
    is "Is Trigger" checked?
    it should not be.
     
  3. Caelorum

    Caelorum

    Joined:
    Feb 6, 2014
    Posts:
    32
    It isn't. I think the problem might be that the larger block is using transform.position to move. But i don't see how I can make it follow the capsule without parenting it or using transform.position.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,557
    If you're warping the physics object from position to position by explicitly setting the transform.position then you're not using continuous collision detection as the object is not actually moving (it has no velocity). It'll only detect collisions where you place it.

    You should use Rigidbody2D.MovePosition instead.
     
    Caelorum likes this.
  5. Caelorum

    Caelorum

    Joined:
    Feb 6, 2014
    Posts:
    32
    Thank you for awaring me on rigidbody2d.moveposition. However, it seems to be an incredibly stuttery command. I have it in update, and i'm telling it to move its position to mimic the position of another object every frame. And it seems to have a hard time doing that in any smooth way.
    It is moving along the vector2 i've commanded, but shaking violently.

    Ex script:

    Code (CSharp):
    1. public class Shield : MonoBehaviour
    2. {
    3.     GameObject shieldBase;
    4.     Transform rotation;
    5.     Vector2 shieldPos;
    6.     Rigidbody2D rb;
    7.     void Awake()
    8.     {
    9.         shieldBase = GameObject.FindGameObjectWithTag ("Follow");
    10.         rb = GetComponent<Rigidbody2D>();
    11.     }
    12.     void Update()
    13.     {
    14.         ShieldMovement ();
    15.     }
    16.  
    17.     void ShieldMovement()
    18.     {
    19.         rotation = shieldBase.transform;
    20.         shieldPos = shieldBase.transform.position;
    21.         transform.rotation = rotation.rotation;
    22.         rb.MovePosition (shieldPos);
    23.     }
    24.  
    25. }