// Update is called once per frame void Update() { //Debug.Log(behavior); if (!camControl.gamePaused) { if (agent.enabled && agent.isStopped) { agent.isStopped = false; anim.speed = animResumeSpeed; } rigid.constraints &= ~RigidbodyConstraints.FreezePosition; currentWaypoint = waypoints[waypointIndex]; float distToTarget = Mathf.Abs(Vector3.Distance(this.transform.position, target.position)); if (!tedBody.dead && sight.iSeeYou && alertLevel < 100) { alertLevel += Time.deltaTime * alertMultiplier; } if (gotPinched) { SetAnimatorToIdle(5, 5, 5, 5, 5, 5, 5); agent.enabled = false; anim.SetTrigger("captured"); } switch (behavior) { case State.Idle: CheckLevels(); SendStatusToID(); if (agent.enabled) { agent.destination = this.transform.position; } if (friendlinessLevel > friendlinessThreshold) { behavior = State.Friendly; } if (alertLevel > alertThreshold) { behavior = State.Alerted; Debug.Log(behavior); } if (motivationLevel > motivationThreshold) { behavior = State.Patrolling; } if (curiosityLevel > curiosityThreshold && friendlinessLevel < friendlinessThreshold) { behavior = State.Looking; } break; case State.Looking: CheckLevels(); SendStatusToID(); if (Time.time - lookTimerStart > lookTimer) { //isLooking = true; anim.SetBool("look", true); speech.SayLookLine(Random.Range(1f, 1.5f)); lookTimerStart = Time.time; lookTimer = Random.Range(lookTimerMin, lookTimerMax) + lookTimerAnimationDelay; } else if (Time.time - lookTimerStart > lookTimerAnimationDelay) { anim.SetBool("look", false); } if (curiosityLevel < curiosityThreshold) { SetAnimatorToIdle(motivationThreshold, curiosityLevel, 0, 0, 0, initialFriendliness, initialHumor); } if (alertLevel > alertThreshold) { Debug.Log(friendlinessLevel + ", " + behavior); if (friendlinessLevel > friendlinessThreshold) { behavior = State.Friendly; Debug.Log(behavior); } else { behavior = State.Alerted; } } break; case State.Patrolling: CheckLevels(); SendStatusToID(); agent.speed = 45; agent.destination = currentWaypoint.position; target = currentWaypoint; if (Mathf.Abs(Vector3.Distance(this.transform.position, target.position)) < 50) { if (waypointIndex < waypoints.Length - 1) { waypointIndex++; } else { waypointIndex = 0; } } if (motivationLevel < motivationThreshold) { behavior = State.Idle; } if (alertLevel > alertThreshold) { behavior = State.Alerted; target = ted.transform; agent.destination = target.position; } break; case State.Alerted: CheckLevels(); SendStatusToID(); agent.speed = 30; if (!tedBody.dead && distToTarget < 100) { behavior = State.Pursuing; threatLevel = threatThreshold + 1; alertLevel = statMaxValue; } if (!sight.iSeeYou && alertLevel > 0) { alertLevel -= Time.deltaTime * (alertMultiplier); } if (alertLevel < alertThreshold) { if (motivationLevel > motivationThreshold) { behavior = State.Patrolling; alertLevel = 0; } else if (motivationLevel < 20) { behavior = State.Looking; alertLevel = 0; } } else if (threatLevel > threatThreshold) { behavior = State.Pursuing; alertLevel = statMaxValue; } break; case State.Pursuing: CheckLevels(); SendStatusToID(); target = ted.transform; if (tedBody.dead) { SetAnimatorToIdle(initialMotivation, initialCuriosity, initialAlert, initialThreat, initialFear, initialFriendliness, initialHumor); } if (!sight.iSeeYou && threatLevel > 0) { threatLevel -= Time.deltaTime * (alertMultiplier); } for (int i = 0; i < numOfFriends; i++) { if ((contacted) && (!friendAI[i].contacted) && Mathf.Abs(Vector3.Distance(this.transform.position, friendAI[i].transform.position)) < 600) { friendAI[i].alertLevel = 100; friendAI[i].motivationLevel = motivationThreshold; friendAI[i].threatLevel = statMaxValue; } } contacted = true; if (threatLevel < threatThreshold) { behavior = State.Alerted; target = ted.transform; agent.destination = target.position; for (int i = 0; i < numOfFriends; i++) { if (((friendAI[i].contacted) && (Mathf.Abs(Vector3.Distance(this.transform.position, target.transform.position)) < (Mathf.Abs(Vector3.Distance(friendAI[i].transform.position, target.transform.position))))) || tedBody.dead) { friendAI[i].SetAnimatorToIdle(initialMotivation, initialCuriosity, initialAlert, initialThreat, initialFear, initialFriendliness, initialHumor); Debug.Log("friend #" + i + " set to idle"); friendAI[i].contacted = false; } } contacted = false; } if (Mathf.Abs(Vector3.Distance(this.transform.position, ted.transform.position)) > 60) { agent.speed = 60; //anim.speed = 1.2f; agent.destination = target.position; } else if (!tedBody.dead) { if (!hitTed && sight.iSeeYou && ((Time.time - timeStartAttack > 0.7) && (Time.time - timeStartAttack < 0.8))) { tedBody.health -= 20; tedBody.numberOfAttacks += 1; hitTed = true; Debug.Log("Attacked " + tedBody.numberOfAttacks + " times. Health: " + tedBody.health + ". Dead yet??? " + tedBody.dead + "."); } else if (Time.time - timeStartAttack > 2) { behavior = State.Attacking; //anim.speed = 2; anim.SetTrigger("attack now"); timeStartAttack = Time.time; hitTed = false; } } break; case State.Attacking: CheckLevels(); SendStatusToID(); anim.SetTrigger("attack complete"); behavior = State.Pursuing; break; case State.Evading: CheckLevels(); SendStatusToID(); break; case State.Friendly: if (alertLevel > alertThreshold && distToTarget < minTalkDistance) { this.transform.LookAt(ted.transform); } break; case State.WeirdingOut: CheckLevels(); SendStatusToID(); break; } } else { rigid.constraints = RigidbodyConstraints.FreezeAll; if (!agent.isStopped) { animResumeSpeed = anim.speed; } agent.isStopped = true; anim.speed = 0; } }
// Update is called once per frame void Update() { if (!camControl.gamePaused) { if (agent.isStopped) { agent.isStopped = false; anim.speed = animResumeSpeed; } rigid.constraints &= ~RigidbodyConstraints.FreezePosition; currentWaypoint = waypoints[waypointIndex]; float distToTarget = Mathf.Abs(Vector3.Distance(this.transform.position, target.position)); //if (!tedBody.dead && sight.iSeeYou && alertLevel < 100) { // alertLevel += Time.deltaTime * alertMultiplier; //} switch (behavior) { case State.Deactivated: if (Time.time - refFakeTedFadeTime > fakeTedFadeTime) { this.transform.position = ted.transform.TransformPoint(spawnPointFromTed); anim.SetTrigger("end fade"); DeactivateFakeTed(); } else { foreach (SkinnedMeshRenderer skin in renderers) { if (skin.material.GetColor("_Color").a > 0) { foreach (Material mat in skin.materials) { Color origColor = mat.GetColor("_Color"); Color newColor = new Color(origColor.r, origColor.g, origColor.b, origColor.a - fakeTedFadeStep); mat.SetColor("_Color", newColor); } } } } break; case State.Idle: CheckLevels(); SendStatusToID(); this.transform.position = ted.transform.TransformPoint(spawnPointFromTed); if (activated) { if (friendlinessLevel > friendlinessThreshold) { behavior = State.Friendly; Debug.Log(behavior); } if (alertLevel > alertThreshold) { behavior = State.Alerted; Debug.Log(behavior); } if (motivationLevel > motivationThreshold) { behavior = State.Patrolling; Debug.Log(behavior); } if (curiosityLevel > curiosityThreshold && friendlinessLevel < friendlinessThreshold) { behavior = State.Looking; } } break; case State.Looking: CheckLevels(); SendStatusToID(); if (Time.time - lookTimerStart > lookTimer) { //isLooking = true; anim.SetBool("look", true); speech.SayLookLine(Random.Range(1f, 1.5f)); lookTimerStart = Time.time; lookTimer = Random.Range(lookTimerMin, lookTimerMax) + lookTimerAnimationDelay; } else if (Time.time - lookTimerStart > lookTimerAnimationDelay) { anim.SetBool("look", false); } if (curiosityLevel < curiosityThreshold) { SetAnimatorToIdle(motivationThreshold, curiosityLevel, 0, 0, 0, initialFriendliness, initialHumor); } if (alertLevel > alertThreshold) { Debug.Log(friendlinessLevel + ", " + behavior); if (friendlinessLevel > friendlinessThreshold) { behavior = State.Friendly; Debug.Log(behavior); } else { behavior = State.Alerted; } } break; case State.Patrolling: CheckLevels(); SendStatusToID(); agent.speed = 30; agent.destination = currentWaypoint.position; target = currentWaypoint; if (Mathf.Abs(Vector3.Distance(this.transform.position, target.position)) < 50) { if (waypointIndex < waypoints.Length - 1) { waypointIndex++; } else { waypointIndex = 0; } } if (motivationLevel < motivationThreshold) { behavior = State.Idle; } if (alertLevel > alertThreshold) { behavior = State.Alerted; target = ted.transform; agent.destination = target.position; } break; case State.Alerted: CheckLevels(); SendStatusToID(); agent.speed = 30; if (!tedBody.dead && distToTarget < 100) { behavior = State.Pursuing; threatLevel = threatThreshold + 1; alertLevel = statMaxValue; } //if (!sight.iSeeYou && alertLevel > 0) { // alertLevel -= Time.deltaTime * (alertMultiplier); //} if (alertLevel < alertThreshold) { if (motivationLevel > motivationThreshold) { behavior = State.Patrolling; alertLevel = 0; } else if (motivationLevel < 20) { behavior = State.Looking; alertLevel = 0; } } else if (threatLevel > threatThreshold) { behavior = State.Pursuing; alertLevel = statMaxValue; } break; case State.Pursuing: CheckLevels(); SendStatusToID(); target = ted.transform; if (tedBody.dead) { SetAnimatorToIdle(initialMotivation, initialCuriosity, initialAlert, initialThreat, initialFear, initialFriendliness, initialHumor); } //if (!sight.iSeeYou && threatLevel > 0) { // threatLevel -= Time.deltaTime * (alertMultiplier); //} contacted = true; if (threatLevel < threatThreshold) { behavior = State.Alerted; target = ted.transform; agent.destination = target.position; contacted = false; } //if (Mathf.Abs(Vector3.Distance(this.transform.position, ted.transform.position)) > 60) { // agent.speed = 60; // //anim.speed = 1.2f; // agent.destination = target.position; //} else if (!tedBody.dead) { // if (!hitTed && sight.iSeeYou && ((Time.time - timeStartAttack > 0.7) && (Time.time - timeStartAttack < 0.8))) { // tedBody.health -= 20; // tedBody.numberOfAttacks += 1; // hitTed = true; // Debug.Log("Attacked " + tedBody.numberOfAttacks + " times. Health: " + tedBody.health + ". Dead yet??? " + tedBody.dead + "."); // } else if (Time.time - timeStartAttack > 2) { // behavior = State.Attacking; // //anim.speed = 2; // anim.SetTrigger("attack now"); // timeStartAttack = Time.time; // hitTed = false; // } //} break; case State.Attacking: CheckLevels(); SendStatusToID(); anim.SetTrigger("attack complete"); behavior = State.Pursuing; break; case State.Evading: CheckLevels(); SendStatusToID(); break; case State.Friendly: if (alertLevel > alertThreshold && distToTarget < minTalkDistance) { this.transform.LookAt(ted.transform); } break; case State.WeirdingOut: CheckLevels(); SendStatusToID(); break; } } else { rigid.constraints = RigidbodyConstraints.FreezeAll; if (!agent.isStopped) { animResumeSpeed = anim.speed; } agent.isStopped = true; anim.speed = 0; } }