// Update is called once per frame void Update() { if (target) { switch (state) { case TuckerState.FOLLOW: if(target.tag == "Player") { if(Vector2.Distance(transform.position, target.transform.position) > 2f && notWithin ()) { //if not right next to player, follow follow (); } else { Debug.Log ("in else of follow player"); } } else if (target.tag == "enemy") { if (Vector2.Distance (transform.position, target.transform.position) > 2f || notWithin ()) { //if not right next to player, follow follow (); if(!barkOnCD && Random.Range(0, 20) > 18) { bark (); barkOnCD = true; StartCoroutine (barkOffCD ()); } } else { if (!attackOnCD) { state = TuckerState.ATTACK; attackOnCD = true; StartCoroutine (attackOffCD ()); } } } break; case TuckerState.ATTACK: //If the target still exists... //Move toward the target. Collision constitutes attacking. if(!barkOnCD) { bark (); barkOnCD = true; StartCoroutine (barkOffCD ()); } transform.position = Vector3.MoveTowards (transform.position, target.transform.position, .3f); //If you somehow get too far away, follow again. if (Vector2.Distance (transform.position, target.transform.position) > 2f || notWithin ()) { state = TuckerState.FOLLOW; } break; case TuckerState.FLY: break; case TuckerState.JUMP: break; } if (target.transform.position.x < transform.position.x && towardTarget != -1) { //if target switches sides, update path updatePathOnce (); towardTarget = -1; } else if (target.transform.position.x > transform.position.x && towardTarget != 1) { updatePathOnce (); towardTarget = 1; } } else target = GameObject.Find ("Player"); }
void OnCollisionEnter2D(Collision2D col) { Collider2D colObj = col.collider; if(colObj.tag == "enemy") { colObj.gameObject.GetComponent<Enemy>().Hurt(10f); if(transform.position.x - colObj.transform.position.x > 0) { colObj.gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(-200, 375)); rigbod.AddForce (new Vector2(200, 375)); } else if(transform.position.x - colObj.transform.position.x < 0) { colObj.gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(200, 375)); rigbod.AddForce (new Vector2(-200, 375)); } colObj.gameObject.GetComponent<SpriteRenderer>().color = Color.red; } state = TuckerState.FOLLOW; }