Example #1
0
    // Update is called once per frame
    void Update()
    {
        rayDirection = (player.transform.position - transform.position);
        bool raycastdown = Physics.Raycast(transform.position, rayDirection, out hit);

        if (raycastdown && hit.transform.name.Equals("Player"))
        {
            state = NPC_STATES.Chase;
        }
        else
        {
            state = NPC_STATES.Patrol;
        }

        switch (state)
        {
        case NPC_STATES.Chase:

            agent.SetDestination(player.position);

            break;

        case NPC_STATES.Patrol:

            agent.SetDestination(destination);
            if (Vector3.Distance(transform.position, destination) < 0.4f)
            {
                Patrolling();
            }

            break;
        }
        Debug.Log(state);
    }
Example #2
0
    // Start is called before the first frame update
    void Start()
    {
        state       = NPC_STATES.Patrol;
        agent       = gameObject.GetComponent <NavMeshAgent>();
        destination = moveSpot.transform.position;

        player = GameObject.FindGameObjectWithTag("Player").transform;
    }