Example #1
0
 private void OnTriggerEnter(Collider other)
 {
     if (other.tag == "Player")
     {
         currentState = agentStates.Aggro;
     }
 }
Example #2
0
    IEnumerator WaitForResetRotation()
    {
        previousRotation   = transform.rotation;
        isResetingRotation = true;
        slerpTimer         = 0.0f;
        yield return(new WaitUntil(() => Quaternion.Angle(transform.rotation, startRotation) <= 1e-3f));

        isResetingRotation = false;
        agentState         = agentStates.idle;
    }
Example #3
0
 // Start is called before the first frame update
 void Start()
 {
     startPosition        = transform.position;
     startRotation        = transform.rotation;
     enemyCleaned         = false;
     isResetingRotation   = false;
     currentWaypointIndex = 0;
     if (Random.Range(0.0f, 1.0f) <= generateEnemyChances)
     {
         triangleType = 0;
         damageDealt  = 10;
     }
     else
     {
         triangleType = 1;
         damageDealt  = -10;
     }
     agentState    = agentStates.idle;
     player        = GameObject.Find("Player");
     agent         = GetComponent <NavMeshAgent>();
     lastSpeakTime = Time.time;
     agentState    = agentStates.idle;
 }
Example #4
0
 // Start is called before the first frame update
 void Start()
 {
     agent           = GetComponent <NavMeshAgent>();
     currentState    = agentStates.Idle;
     defaultRestTime = pointRestTime;
 }
Example #5
0
    // Update is called once per frame
    void Update()
    {
        if (!enemyCleaned || triangleType == 1)
        {
            distanceToPlayer      = Vector3.Distance(transform.position, player.transform.position);
            distanceStartToPlayer = Vector3.Distance(startPosition, player.transform.position);
            if (distanceToPlayer <= chaseDistance && distanceToPlayer > talkDistance && distanceStartToPlayer <= maxChaseRange)
            {
                if (player.GetComponent <PlayerController>().isMuted)
                {
                    if (agentState == agentStates.chase)
                    {
                        agentState = agentStates.backoff;
                        agent.SetDestination(transform.position + (transform.position - player.transform.position).normalized * 2);
                    }
                }
                else if (agentState != agentStates.backoff)
                {
                    agentState = agentStates.chase;
                    agent.SetDestination(player.transform.position);
                }
            }
            else if (distanceToPlayer <= talkDistance && distanceStartToPlayer <= maxChaseRange)
            {
                if (player.GetComponent <PlayerController>().isMuted)
                {
                    if (agentState == agentStates.chase)
                    {
                        agentState = agentStates.backoff;
                        agent.SetDestination(transform.position + (transform.position - player.transform.position).normalized * 2);
                    }
                }
                else if (agentState != agentStates.backoff)
                {
                    agentState = agentStates.chase;
                    agent.SetDestination(transform.position);
                    transform.LookAt(player.transform.position);
                }

                if (Time.time - lastSpeakTime >= speakCD)
                {
                    lastSpeakTime = Time.time;
                    if (!player.GetComponent <PlayerController>().isMuted)
                    {
                        player.GetComponent <PlayerController>().takeDamage(damageDealt);
                    }
                    //if(triangleType == 0)
                    transform.GetChild(0).gameObject.GetComponent <EnemySounds>().playVoice(triangleType);
                }
            }
            else
            {
                if (agentState == agentStates.chase)
                {
                    agent.SetDestination(startPosition);
                    agentState = agentStates.backToStartPosition;
                }
            }


            //AI movement pattern
            if (agentState == agentStates.idle)
            {
                switch (wanderMode)
                {
                case "Circle":
                    angle += rotationStep * Time.deltaTime;
                    Vector3 offset = new Vector3(Mathf.Sin(angle), 0, Mathf.Cos(angle)) * circleRotationRadius;
                    agent.SetDestination(startPosition + centerOffset + offset);
                    break;


                case "Linear":
                    if (agent.remainingDistance <= 0.01f)
                    {
                        agent.SetDestination(waypoints[currentWaypointIndex++]);
                    }
                    if (currentWaypointIndex >= waypoints.Length)
                    {
                        currentWaypointIndex = 0;
                    }
                    break;

                case "Static":
                    break;


                default:
                    break;
                }
            }

            if (agent.remainingDistance <= 0.01f)
            {
                switch (agentState)
                {
                case agentStates.backoff:
                    agentState = agentStates.backToStartPosition;
                    agent.SetDestination(startPosition);
                    break;

                case agentStates.backToStartPosition:
                    StartCoroutine(WaitForResetRotation());
                    agentState = agentStates.backToStartRotation;
                    break;

                default:
                    break;
                }
            }

            if (isResetingRotation)
            {
                Debug.Log(slerpTimer);
                transform.rotation = Quaternion.Slerp(previousRotation, startRotation, slerpTimer);
                slerpTimer        += resetRotationSpeed;
            }
        }
        else
        {
            agent.SetDestination(transform.position);
        }
    }