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

2D Collision Issues

Discussion in 'Scripting' started by jlpeyton, Feb 4, 2020.

  1. jlpeyton

    jlpeyton

    Joined:
    Dec 22, 2017
    Posts:
    57
    I have a sphere prefab that spawns several of these objects at a starting point. I'm trying to get the spheres to fall and then begin rotating back and forth when they collide with the walls. The spheres should not collide with each other. When I test with a single sphere, it rotates back and forth several times but will eventually get stuck at the wall or completely ignore the collision and fall off the game screen. When I add several spheres to the mix things just go crazy. All I want is a smooth drop down and then begin rotating left and right. Each cube has a collider called "Floor" and when the sphere collides with the floor it should stop falling and begin rotating. Each cube also has a child object with a collider called "Wall". When the sphere collides with this it changes direction. Why are these objects bouncing around so much or completely ignoring collisions?

    Video:


    Code (CSharp):
    1. public class FleaManager : MonoBehaviour
    2. {
    3.     bool moveRight = true;
    4.     bool falling = true;
    5.     public int speed = 3;
    6.  
    7.     // Update is called once per frame
    8.     void Update()
    9.     {
    10.         if (moveRight && !falling)
    11.         {
    12.             transform.Translate(Vector2.right * speed * Time.deltaTime);
    13.         }
    14.         else if (!moveRight && !falling)
    15.         {
    16.             transform.Translate(Vector2.left * speed * Time.deltaTime);
    17.         }
    18.         else
    19.         {
    20.             transform.Translate(Vector2.down * speed * Time.deltaTime);
    21.         }
    22.      
    23.     }
    24.  
    25.     private void OnCollisionEnter2D(Collision2D collision)
    26.     {
    27.         if (collision.gameObject.tag == "Wall" && moveRight)
    28.         {
    29.             Debug.Log("moveright false");
    30.             moveRight = false;
    31.         }
    32.         else
    33.         {
    34.             Debug.Log("moveright true");
    35.             moveRight = true;
    36.         }
    37.  
    38.         if(collision.gameObject.tag == "Floor" && falling)
    39.         {
    40.             Debug.Log("moveright true & falling false");
    41.             moveRight = true;
    42.             falling = false;
    43.         }
    44.     }
    45. }

    The wall has two colliders. The one extended out is tagged as "Wall". The slightly smaller one is tagged "Floor".
    wall.JPG
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Not sure what might be going on, but you gotta admit, they do behave a lot like
    fleas...
    :)

    One thing that comes to mind is your shape size is quite small given the speed of motion. However far they would move in 1/60th or 1/30th of a second or whatever your simulation distance is, that distance should be much smaller than the size of your collider, otherwise your collisions will be quite deeply-embedded and result in huge impulses being calculated.

    You can perhaps get better collisions if you set the collisionDetectionMode to continuous on the Rigidbody2Ds.

    Here's the ref:

    https://docs.unity3d.com/ScriptReference/CollisionDetectionMode2D.html
     
  3. jlpeyton

    jlpeyton

    Joined:
    Dec 22, 2017
    Posts:
    57
    lol :D. You are correct but not the motion I'm going for. I assigned the spheres a layer and adjusted the Physics 2D in the project settings by unchecking that layer so they don't interact with each other. This fixed the scattered motions now they hit the ground and move left to right. Sometimes the spheres will still get stuck against the wall collider. After increasing the size of the wall box collider and reducing the speed of the spheres, I was able to keep this from happening as frequent but it's still not 100%
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Well, sometimes with Physics simulation (in any context), that's about all you can hope for. :)

    You might find success by putting a script on the objects that can observe their conditions and if they note they've become wedged, lift or unwedge them by adding some impulsive force.
     
  5. jlpeyton

    jlpeyton

    Joined:
    Dec 22, 2017
    Posts:
    57
    That's a good idea! I can store the transform.position.x in a variable and monitor whether it changes and if it doesn't, BUMP! Thanks for the help.
     
    Kurt-Dekker likes this.