void OnCollisionEnter2D(Collision2D col) { // do this check so the collision is only called once - failsafe against unity physics engine if (_isColliding) { return; } _isColliding = true; // only check for collisions with the player is the steel crate's gravity is enabled if (gravEnabled && col.gameObject.CompareTag("Player")) { Transform groundCheck = col.gameObject.transform.GetChild(0); Vector2 posOffset = (Vector2)transform.position + PhysicsUtilities.OffsetToEdgeRenderer(rend); // NEED TO FIND OUT HOW TO NOT DESTROY INSTANTLY WHILE IN GRAV TRANSITION // FOR SOME REASON THE BELOW DOES NOT WORK if (!GameController.gravTransitionState && PhysicsUtilities.IsBelow(groundCheck.position, posOffset)) { Destroy(col.gameObject); } // to be added: enter gameover state } // handle check for glass if (isGlass && GameController.gravTransitionState) { if (_glass.CollisionHandler(rend)) { Destroy(gameObject); } } }
// child the object to any valid object that it can be on and store what kind of object that is protected void AttachToTile() { Vector3 offset = PhysicsUtilities.OffsetToEdgeRenderer(GetComponent <SpriteRenderer>(), oppositeFacing); RaycastHit2D hit = Physics2D.Raycast(transform.position + offset, oppositeFacing.ToVector2(), 0.1f, canAttachToLayerMask); if (hit.collider != null) { transform.parent = hit.transform; parentLayer = LayerMask.LayerToName(hit.transform.gameObject.layer); } }
// find out if the object is grounded bool isGrounded() { // for position, get the "bottom" edge in terms of the direction of gravity Vector2 position = (Vector2)transform.position + PhysicsUtilities.OffsetToEdgeRenderer(rend); //Debug.DrawLine(transform.position, position); float dist = 0.1f; RaycastHit2D hit = PhysicsUtilities.RaycastToGravity(position, dist, GameController.terrainLayer); if (hit.collider != null) { return(true); } return(false); }