1
\$\begingroup\$

I am working with the Unity3D Engine in 2D mode. Currently I am trying to make a circle collider bounce off of a composite collider for the walls of my game. The only issue is that for some reason the OnCollisionEnter2D function does not seem to even trigger when the circle collider hits the walls. I have used the log to determine this. I am curious as to why it has this behviour, since it does end up triggering when the player contacts the circle collider? Here is the important parts of the script:

public class sword : MonoBehaviour {

public Rigidbody2D rb;
public CircleCollider2D coll;

public float speed;
public Vector2 trajectory;
public GameObject Player;




// Use this for initialization
void Start () {
    speed = 0.0f;
    anim = Player.GetComponent<Animator>();
}



private void OnCollisionEnter2D(Collision2D collision)
{
    Debug.Log("collided");
    if (collision.gameObject.tag == "player")
    {
        transform.position = new Vector2(-1000, -1000);
        Player.GetComponent<movement>().equipped = true;
    }
    if (collision.gameObject.tag == "solid")
    {
        ContactPoint2D otherObject = collision.contacts[0];

        Vector2 hit = otherObject.point;
        Vector2 normal = otherObject.normal;
        trajectory = Vector2.Reflect(trajectory, normal);

    }
}

private void FixedUpdate()
{
    //constantly calculating new velocity
    rb.velocity = speed * trajectory;
    speed = speed * 0.93f; //drag
    if (speed < 0.0001f)
    {
        speed = 0.0f;
    }
    if (speed > 1)
    {
        Physics2D.IgnoreCollision(Player.GetComponent<CircleCollider2D>(), coll, true);
    }
    else
    {
        Physics2D.IgnoreCollision(Player.GetComponent<CircleCollider2D>(), coll, false);
    }
}
}

More Details: My speed variable is set from a separate script, in case you are wondering. Everything is on the layer 0.. The Rigidbody for player is dynamic, for the ball it is kinematic, and for the walls it is static. Also here is an image of what the game screen would look like, I've labelled the walls and the circle collider (used for a sword).

enter image description here

Thank you.

\$\endgroup\$
  • \$\begingroup\$ Can you give us some details about the rigid bodies and the colliders, particularly is it kinematic and what layers is everything on. \$\endgroup\$ – mr-matt Nov 20 '18 at 4:31
  • \$\begingroup\$ Right, for sure! I added the info into the "More Details" part at the bottom. Basically it's all on layer 0, and the walls are static, player is dynamic, and sword is kinematic. Hope this helps, and let me know if you need other details. \$\endgroup\$ – Morgan Nov 20 '18 at 4:40
  • \$\begingroup\$ check the tag in gameobject \$\endgroup\$ – Manoj Balaji M Nov 20 '18 at 5:28
0
\$\begingroup\$

So I figured out the issue. It was that the RigidBody on the ball needed "Use full Kinematic Contacts" selected. Now the object collision and reflection works as intended. The only issue now is that the object overlaps with the wall by the velocity for 1 frame before it is seen as "entered" and therefore phases into it a little. This causes a huge issue when it bounces, while still being phased into the collider, and hits another wall. Since it hasn't technically left the collider, OnCollisionEnter2D does not activate when it hits the nearby wall, causing it to simply pass through. The collision detection issue is solved, so I will try to work out this new issue myself or create a different question. Thanks for hinting me that it may be in the RigidBodies.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.