IEnumerator attack(float second) { if (wait) { yield break; } wait = true; yield return(new WaitForSeconds(second)); wait = false; if (intruderDetected) { hitSound.Play(); Debug.Log("INTRUDER DETECTED"); dogC.takeDamage(1); } }
// Update is called once per frame void Update() { timesincelastblock += Time.deltaTime; timesincelastjump += Time.deltaTime; transform.position = new Vector3(transform.position.x, transform.position.y, 0f); if (!isDead) { anim.SetBool("isDead", false); Vector3 right = transform.position + Vector3.right * transform.lossyScale.x * 0.15f; Vector3 left = transform.position - Vector3.right * transform.lossyScale.x * 0.15f; Debug.DrawLine(right, right + (Vector3.down * transform.lossyScale.y * 0.62f)); Debug.DrawLine(left, left + (Vector3.down * transform.lossyScale.y * 0.62f)); onSomething = Physics.Linecast(right, right + (Vector3.down * transform.lossyScale.y * 0.62f), 1 << LayerMask.NameToLayer("Obstacle") | 1 << LayerMask.NameToLayer("Enemy")) || Physics.Linecast(left, left + (Vector3.down * transform.lossyScale.y * 0.62f), 1 << LayerMask.NameToLayer("Obstacle") | 1 << LayerMask.NameToLayer("Enemy")); dogDistance = Vector3.Distance(dog.transform.position, transform.position); RaycastHit rh; bool blocked = false; eyePosition = transform.position + Vector3.up * 0.666f; feetPosition = transform.position + Vector3.down * 1f; Debug.DrawLine(feetPosition, feetPosition + (Vector3.right * direction).normalized * 4f, Color.red); Vector3 dogDirection = dog.transform.position - eyePosition; if (Physics.Raycast(feetPosition, Vector3.right * direction, out rh, 4f, ~(1 << LayerMask.NameToLayer("Interactable") | 1 << LayerMask.NameToLayer("Enemy") | 1 << LayerMask.NameToLayer("Dog")))) { if (rh.collider.tag == "Thingy" && !isDead && !jump) { blocked = true; timesincelastblock = 0f; } } if (seesDog) { if (blocked && !jump && onSomething && timesincelastjump > 0.05f) { jump = true; timesincelastjump = 0f; } focusDirection = dogDirection; dogAngle = 180; speed = 7f; if (Vector3.Distance(transform.position, dog.transform.position) < 3f) { speed = 0f; // nearDog = true; } viewDistance = 10f; if (dogDirection.x < 0) { direction = -1; } else { direction = 1; } attentionCountdown = 2f; } else { if (attentionCountdown < 0f) { if (dogAngle > initialDogAngle) { dogAngle = dogAngle - 5; } else if (dogAngle < initialDogAngle) { dogAngle = initialDogAngle; } focusDirection = new Vector3(direction, -0.9f, 0f); // dogAngle = initialDogAngle; speed = initialSpeed; viewDistance = initialviewDistance; // notice.GetComponent<MeshRenderer>().enabled = false; dogC.observedArray[num - 1] = true; if (blocked && !jump && onSomething && timesincelastjump > 0.1f) { if (timesincelastblock > 0.3f) { if (Random.value > 0.8f) { jump = true; timesincelastjump = 0f; } } else if (Random.value > 2f * timesincelastblock + 0.2f) { jump = true; timesincelastjump = 0f; } else { direction = direction * -1; } } } else { speed = 0f; attentionCountdown -= Time.deltaTime; } } if (gettingHit) { speed = 0f; } seesDog = false; if (Vector3.Distance(dog.transform.position, transform.position) < 2f) { seesDog = true; //Debug.Log("I see you!"); dogC.observedArray[num - 1] = false; } // if (Vector3.Angle (dogDirection, Vector3.right * direction + Vector3.down) < dogAngle) { // if (Physics.Raycast (eyePosition, dogDirection.normalized * viewDistance, out rh, viewDistance, ~(1 << LayerMask.NameToLayer ("Interactable") | 1 <<LayerMask.NameToLayer("Enemy")))) { // if (rh.collider.tag == "Dog") { //// Debug.Log ("I SAW THE F*****G DOG"); // seesDog = true; // dogC.observedArray[num-1] = false; // notice.GetComponent<MeshRenderer>().enabled = true; // Vector3 temp = new Vector3(transform.position.x,transform.position.y + 3,0); // notice.transform.position = temp; // //Debug.Log("SEEN!"); // } // } // } // Debug.DrawRay (eyePosition, dogDirection.normalized * viewDistance, Color.green); // Debug.DrawRay (eyePosition, Vector3.right * direction * viewDistance, Color.red); // Debug.DrawRay (eyePosition, Vector3.down * viewDistance, Color.red); transform.position = new Vector3(transform.position.x, transform.position.y, 0f); myRb.velocity = new Vector3(direction * speed, myRb.velocity.y, myRb.velocity.z); Vector3 s = transform.localScale; s.x = direction * 2f; transform.localScale = s; // Debug.Log (myType); if (myType == 0) { if (nearDog) { attackCD -= Time.deltaTime; } // Debug.Log ("(" + (nearDog) + "|" + (attackCD <= 0f) + "|" + (!isDead) + "|" + (!(dogC.gettingHit)) + "|" + (seesDog) + "|" + (!gettingHit)); if (nearDog && attackCD <= 0f && !isDead && !(dogC.gettingHit) && !gettingHit) { Debug.Log("I ATTACKED"); hitSound.Play(); dogC.takeDamage(1); dogC.myRb.AddForce(new Vector3(Mathf.Sign((dog.transform.position - transform.position).x) * 1000f, 1000f, 0f)); attackCD = 1f; isAttacking = true; timesincelastattack = 0f; anim.SetBool("isAttacking", isAttacking); } if (dogC.gettingHit) { attackCD = 1f; } } if (dogDistance < 50) { castRays(); updateMesh(); updateMeshMaterial(); } } else { // transform.rotation = Quaternion.Euler (0f, 0f, 90f); // if (timesincedeath < 0.01f) { // myRb.AddForce ((Vector3.up * 5f + Vector3.right * Mathf.Sign((transform.position - dog.transform.position).x) * 3f).normalized * 300f); // // myRb.AddForce ((Vector3.up * 5f).normalized * 300f); // } anim.SetBool("isDead", true); timesincedeath += Time.deltaTime; dogC.observedArray[num - 1] = true; gameObject.layer = 9; meshRenderer.enabled = false; // myRb.freezeRotation = false; myRb.AddTorque(new Vector3(70f, 0f, 0f)); // myRb.AddTorque (new Vector3 (0f, 0f, direction * 90f)); // myRb.rotation = (Quaternion.Lerp(Quaternion.Euler(0f, 0f, 0f), Quaternion.Euler(0f, 0f, direction * 90f), timesincedeath * 2.5f)); // transform.position = new Vector3 (transform.position.x, transform.position.y, 10f); } // if (timesincelastattack < 0.1f) { timesincelastattack += Time.deltaTime; // } if (speed == 0) { anim.SetBool("isIdling", true); anim.SetBool("isWalking", false); } else { anim.SetBool("isIdling", false); anim.SetBool("isWalking", true); } if (isAttacking) { // Debug.Log ("IAMATTACKING"); timesincelastattack += Time.deltaTime; if (timesincelastattack > 0.2f) { isAttacking = false; } } if (gettingHit) { speed = 0f; timesincelasthit += Time.deltaTime; if (timesincelasthit > 0.3f) { gettingHit = false; } } anim.SetBool("isHit", gettingHit); anim.SetBool("isAttacking", isAttacking); }
// Update is called once per frame void Update() { if (!isDead) { RaycastHit rh; Vector3 eyePosition = transform.position + Vector3.up * 0.666f; Vector3 dogDirection = dog.transform.position - eyePosition; //Debug.Log(attentionCountdown); if (seesDog) { dogAngle = 181f; speed = 9f; if (Vector3.Distance(transform.position, dog.transform.position) < 2f) { speed = 0f; } dogDistance = 10f; if (dogDirection.x < 0) { direction = -1; } else { direction = 1; } attentionCountdown = 2f; } else { if (attentionCountdown < 0f) { dogAngle = initialDogAngle; speed = initialSpeed; dogDistance = initialDogDistance; } else { speed = 0f; attentionCountdown -= Time.deltaTime; } } seesDog = false; if (Vector3.Distance(dog.transform.position, transform.position) < 2f) { seesDog = true; dogC.isObserved = true; } if (Vector3.Angle(dogDirection, Vector3.right * direction + Vector3.down) < dogAngle) { if (Physics.Raycast(eyePosition, dogDirection.normalized * dogDistance, out rh, dogDistance, ~(1 << LayerMask.NameToLayer("Interactable")))) { if (rh.collider.tag == "Dog") { // Debug.Log ("I SAW THE F*****G DOG"); seesDog = true; dogC.isObserved = true; //Debug.Log("SEEN!"); } } } Debug.DrawRay(eyePosition, dogDirection.normalized * dogDistance, Color.green); Debug.DrawRay(eyePosition, Vector3.right * direction * dogDistance, Color.red); Debug.DrawRay(eyePosition, Vector3.down * dogDistance, Color.red); transform.position = new Vector3(transform.position.x, transform.position.y, 0f); myRb.velocity = new Vector3(direction * speed, myRb.velocity.y, myRb.velocity.z); Vector3 s = transform.localScale; s.x = direction; transform.localScale = s; if (myType == 0) { if (nearDog) { attackCD -= Time.deltaTime; } if (nearDog && attackCD <= 0f && !isDead) { Debug.Log("I ATTACKED"); hitSound.Play(); dogC.takeDamage(1); attackCD = 1f; } } } else { transform.rotation = Quaternion.Euler(0f, 0f, 90f); dogC.isObserved = false; } }