Search code examples
c#unity-game-enginecollision

Unity. Disable collision physics but have method for collision called


Basically, i have player object and bunch of other. For this moment they all have RigitBody2D, and colliders, code for creating:

        obj = new GameObject();
        obj.tag = "web";    
        obj.name = "web";
        float randomX = Random.Range (Utilities.getMainCameraBounds ().min.x, Utilities.getMainCameraBounds ().max.x);
        float randomY = Random.Range (Utilities.getMainCameraBounds ().min.y, Utilities.getMainCameraBounds ().max.y);
        obj.transform.position = new Vector3(randomX, randomY);
        obj.transform.localScale = new Vector3 (3.0f, 3.0f, 3.0f);
        collider = obj.AddComponent <BoxCollider2D>();
        body = obj.AddComponent<Rigidbody2D> ();
        body.constraints = RigidbodyConstraints2D.FreezeAll;
        joint = obj.AddComponent<HingeJoint2D> ();

        renderer = obj.AddComponent<SpriteRenderer>();

        renderer.sortingOrder = 1;

        renderer.sprite = Resources.Load("Textures/dot_net", typeof(Sprite)) as Sprite;

Purpose for this is to catch collision like this :

void OnCollisionEnter2D(Collision2D collision) {
    if (collision.transform.tag == "web") {
        Player.getInstance().joint(collision.rigidbody);
    }
}

After player collides with some of those objects, method joint called, it's purpose to connect player and web with HingeJoint2D to simulate pendulum behvior. Code of joint:

public void joint(Rigidbody2D body) {
    WebCollection.getInstance ().turnOffColliders (body.gameObject);
    HingeJoint2D hingeJoint = webLine.getObject ().AddComponent<HingeJoint2D> ();
    hingeJoint.connectedBody = body;
    hingeJoint.anchor = new Vector2 (0.00120592f, 0.1921938f);
    hingeJoint.connectedAnchor = new Vector2 (0.1939605f, 0.03025406f);
}

Pendulum behavior is working just fine, but in moment of it, or just when player is moving towards web, other web objects are colliding with it, creating real colliding behavior(like crushing into each other)

My goal: i want web objects to react on the collision(calling method above), but on the screen to player just go throw them.

What i've already tried: I wanted to turnOff other objects bodies (WebCollection.getInstance ().turnOffColliders (body.gameObject);) and after pendulum movement is over, to turn them back, but it causes very strange behavior.

What also can be a solution is to manually checking if player is colliding with web object right now, and handle it, but this is to costly i think.


Solution

  • To put my comment into an answer for those finding your question:

    If you want to have Colliders that only trigger events and do not physically collide with other Colliders, then what you want are Triggers. To make a Collider a trigger you check the Is Trigger checkbox of the collider component in the inspector (you can also set it via script with the Collider.IsTrigger boolean property).

    To do something when the trigger fires, use the OnTrigger events (OnTriggerEnter, OnTriggerExit, OnTriggerStay) (see http://docs.unity3d.com/ScriptReference/Collider.html)

    There is also a short introduction to Colliders as Triggers by unity: http://unity3d.com/learn/tutorials/topics/physics/colliders-triggers