public override void Awake() { MoveAnimation = MoveAnim.Idle; xScale = this.transform.localScale.x; interactTrigger = transform.Find("Interact").GetComponent<BoxCollider2D>(); bodyCol = GetComponent<CircleCollider2D>(); _MoveInteractTrigger = interactTrigger.GetComponent<MoveInteractTrigger>(); Canvas playerCanvas = GetComponentInChildren<Canvas>(); if (playerCanvas != null) { playerCanvasTransform = playerCanvas.transform; } base.Awake(); }
public void CheckCollision(BoxCollider2D box1, BoxCollider2D box2) { float Box1Left = box1.transform.position.x + (box1.offset.x - box1.size.x * 0.5f) * box1.transform.localScale.x; float Box1Right = box1.transform.position.x + (box1.offset.x + box1.size.x * 0.5f) * box1.transform.localScale.x;// * box2.transform.localScale.x); float Box1Top = box1.gameObject.transform.position.y + (box1.offset.y + box1.size.y * 0.5f) * box1.transform.localScale.y; float Box1Bottom = box1.transform.position.y + (box1.offset.y - box1.size.y * 0.5f) * box1.transform.localScale.y;// * box2.transform.localScale.y); float Box2Left = box2.transform.position.x + (box2.offset.x - box2.size.x * 0.5f) * box2.transform.localScale.x; float Box2Right = box2.transform.position.x + (box2.offset.x + box2.size.x * 0.5f) * box2.transform.localScale.x;// * box2.transform.localScale.x); float Box2Top = box2.gameObject.transform.position.y + (box2.offset.y + box2.size.y * 0.5f) * box2.transform.localScale.y; float Box2Bottom = box2.transform.position.y + (box2.offset.y - box2.size.y * 0.5f) * box2.transform.localScale.y;// * box2.transform.localScale.y); //Debug.Log("STP->box2:" + box2.name + ":Box2Left=" + Box2Left + ":Box2Right=" + Box2Right + "\t:Box2Top=" + Box2Top + "\t:Box2Bottom=" + Box2Bottom + "\tbox2.transform.position.y=" + box2.gameObject.transform.position.y + "\t:box2.offset.y=" + box2.offset.y); bool collisionOnBox2Left = ((Box1Left <= Box2Left) && (Box1Right >= Box2Left)); //collision on box2Left if bool collisionOnBox2Right = ((Box1Left <= Box2Right) && (Box1Right >= Box2Right)); //collision on Box2Right bool collisionOnBox2Top = ((Box1Top >= Box2Top) && (Box1Bottom <= Box2Top)); //collision on Box2Top bool collisionOnBox2Bottom = ((Box1Top >= Box2Bottom) && (Box1Bottom <= Box2Bottom)); //collision on Box2Right bool box1ContainsBox2Horizontally = ((Box1Left >= Box2Left) && (Box1Right <= Box2Right)); bool box1ContainsBox2Vertically = ((Box1Top <= Box2Top) && (Box1Bottom >= Box2Bottom)); //Debug.Log("STP->collisionOnBox2Left=" + collisionOnBox2Left + "\t collisionOnBox2Right=" + collisionOnBox2Right + "\t collisionOnBox2Top=" + collisionOnBox2Top + "\t collisionOnBox2Bottom=" + collisionOnBox2Bottom // + "\t box1ContainsBox2Horizontally=" + box1ContainsBox2Horizontally + "\t box1ContainsBox2Vertically=" + box1ContainsBox2Vertically); bool thereIsACollision = false; if ((collisionOnBox2Right && collisionOnBox2Top) || (collisionOnBox2Right && collisionOnBox2Bottom) || (collisionOnBox2Right && box1ContainsBox2Vertically)) { //Debug.Log("STP->collision true on box 2 right"); thereIsACollision = true; } if ((collisionOnBox2Left && collisionOnBox2Top) || (collisionOnBox2Left && collisionOnBox2Bottom) || (collisionOnBox2Left && box1ContainsBox2Vertically)) { //Debug.Log("STP->collision true on box 2 leftt"); thereIsACollision = true; } if ((box1ContainsBox2Horizontally && collisionOnBox2Top) || (box1ContainsBox2Horizontally && collisionOnBox2Bottom) || (box1ContainsBox2Horizontally && box1ContainsBox2Vertically)) { //Debug.Log("STP->collision true on box 2 contains box 1 horizontally"); thereIsACollision = true; } if (thereIsACollision) { CharacterControllerScript character = box1.GetComponent<CharacterControllerScript>(); if (character != null) { if (box2.tag == "Ground") { character.GroundCollision(); } if (box2.tag == "Finish") { character.FinishCollision(); } if (box2.tag == "BEar") { DOCTORBEAR docBear = box2.GetComponent<DOCTORBEAR>(); if (docBear != null) docBear.OnShowSpeechBubble(); } if (box2.tag == "death") { GameManager.GetInstance().Respawn(); } if (box2.tag == "MeteorSpawner") { MeteorSpawner spawner = box2.GetComponent<MeteorSpawner>(); if (spawner != null) spawner.Spawn(); } } } }
// Use this for initialization void Start() { isOn = false; playerCollider = GameObject.FindGameObjectWithTag("Player").GetComponent<BoxCollider2D>(); player = playerCollider.GetComponent<Player>(); thisCollider = GetComponent<BoxCollider2D>(); }
private void PerformColliderCalculations(BoxCollider2D collider, ref Vector2 position1, ref Vector2 position2) { var script = collider.GetComponent<PositionHelper>(); if (script == null) { Debug.LogError("Detected Enemy does not have a Position Helper component"); return; } var relativePosition = new Vector2(transform.position.x - collider.transform.position.x, transform.position.y - collider.transform.position.y); Resources.RotatePositionVectorAboutOrigin(ref relativePosition, script.AngleOfRotation); var cornerPositions = script.Positions; var cornerRelativePositions = script.RelativePositionsToCenter; if (Resources.IsBetween(relativePosition.x, cornerRelativePositions._topLeft.x, cornerRelativePositions._bottomRight.x)) { if (relativePosition.y > cornerRelativePositions._topLeft.y) { position1 = cornerPositions._topLeft; position2 = cornerPositions._topRight; _backgroundManager.AddPositions(position1, position2); return; } if (relativePosition.y < cornerRelativePositions._bottomRight.y) { position1 = cornerPositions._bottomLeft; position2 = cornerPositions._bottomRight; _backgroundManager.AddPositions(position1, position2); return; } } if (Resources.IsBetween(relativePosition.y, cornerRelativePositions._bottomRight.y, cornerRelativePositions._topRight.y)) { if (relativePosition.x > cornerRelativePositions._topRight.x) { position1 = cornerPositions._bottomRight; position2 = cornerPositions._topRight; _backgroundManager.AddPositions(position1, position2); return; } if (relativePosition.x < cornerRelativePositions._bottomRight.x) { position1 = cornerPositions._bottomLeft; position2 = cornerPositions._topLeft; _backgroundManager.AddPositions(position1, position2); return; } } if (relativePosition.x > cornerRelativePositions._topRight.x && relativePosition.y > cornerRelativePositions._topRight.y) { position1 = cornerPositions._topLeft; position2 = cornerPositions._bottomRight; _backgroundManager.AddPositions(position1, position2); return; } if (relativePosition.x > cornerRelativePositions._bottomRight.x && relativePosition.y < cornerRelativePositions._bottomRight.y) { position2 = cornerPositions._topRight; position1 = cornerPositions._bottomLeft; _backgroundManager.AddPositions(position1, position2); return; } if (relativePosition.x < cornerRelativePositions._bottomLeft.x && relativePosition.y < cornerRelativePositions._bottomLeft.y) { position1 = cornerPositions._topLeft; position2 = cornerPositions._bottomRight; _backgroundManager.AddPositions(position1, position2); return; } if (relativePosition.x < cornerRelativePositions._topLeft.x && relativePosition.y > cornerRelativePositions._topLeft.y) { position1 = cornerPositions._topRight; position2 = cornerPositions._bottomLeft; _backgroundManager.AddPositions(position1, position2); return; } }
public void StartTask(string taskName, BoxCollider2D focusArea) { Vector2 aa = Camera.main.WorldToScreenPoint(focusArea.GetComponent<BoxCollider2D>().bounds.min); Vector2 bb = Camera.main.WorldToScreenPoint(focusArea.GetComponent<BoxCollider2D>().bounds.max); Debug.Log("AA" + aa.x); Debug.Log("BB" + bb.x); allExperients[curExperiment].StartTask(taskName, new Vector2((int)aa.x, (int)aa.y), new Vector2((int)bb.x, (int)bb.y)); }