void OnTriggerExit(Collider other) { if (other.gameObject.tag == "Box") { BoxPushedScript other_script = other.gameObject.GetComponent <BoxPushedScript>(); SetActivated(false); other_script.SetState(BoxPushedScript.State.IDLE); } }
// Called as long as there is collission going on (with focus on boxes) void OnTriggerStay(Collider other) { if (other.gameObject.tag == "Box") { BoxPushedScript other_script = other.gameObject.GetComponent <BoxPushedScript>(); if (!other_script.IsMoving()) { if (other.gameObject.GetComponent <MeshRenderer>().materials[0].color == color) { SetActivated(true); other_script.SetState(BoxPushedScript.State.CORRECT); } else { other_script.SetState(BoxPushedScript.State.WRONG); } } else { SetActivated(false); other_script.SetState(BoxPushedScript.State.IDLE); } } }
// Checks for collision in front of pusher, and if there's an obstacle, behaves accordingly public bool CollisionCheckInFront(Vector3 direction) { bool collision = false; BoxPushedScript box_script = null; CharacterControllerScript char_script = null; RaycastHit hit = new RaycastHit(); Vector3 pos = rb.position; if (gameObject.tag == "Elevator") { if (direction.y > 0) { pos.y -= 0.1f; } else { pos.y += 1.1f; } } else { pos.y += 0.1f; } Physics.Raycast(pos, direction, out hit, Utility.GRID_SIZE); if (hit.collider != null && hit.distance < Utility.GRID_SIZE) { // Nothing can move through walls collision |= (hit.collider.gameObject.tag == "Wall"); // Boxes can't move through goals collision |= (gameObject.tag == "Box" && hit.collider.gameObject.tag == "Goal"); // Boxes can't move through other boxes collision |= (gameObject.tag == "Box" && hit.collider.gameObject.tag == "Box"); // Boxes or character don't fall through elevators collision |= ((gameObject.tag == "Box" || gameObject.name == "Character") && hit.collider.gameObject.tag == "Elevator"); // Character can push a box, thus being able to continue move, // IF the box does not collide, in turn if (gameObject.name == "Character" && hit.collider.gameObject.tag == "Box") { box_script = hit.collider.gameObject.GetComponent <BoxPushedScript>(); if (!box_script.falling) { box_script.CheckForFall(); } collision |= box_script.IsFalling(); collision |= box_script.CollisionCheckInFront(direction); //Debug.Log("Character vs box, box falling: "+box_script.IsFalling()+", box pos: "+hit.rigidbody.position+", character pos: "+rb.position); if (!collision) { box_script.Pushed(gameObject); gameObject.GetComponent <CharacterControllerScript>().pushing = true; } } if (gameObject.tag == "Elevator") { if (hit.collider.gameObject.tag == "Box") { box_script = hit.collider.gameObject.GetComponent <BoxPushedScript>(); box_script.Pushed(gameObject); } else if (hit.collider.gameObject.name == "Character") { char_script = hit.collider.gameObject.GetComponent <CharacterControllerScript>(); char_script.Pushed(gameObject); } } //Debug.Log("Collision registered by [" + gameObject.name + "]: " + hit.collider.gameObject.name); } return(collision); }