fixelのブログ

Unity&C#初心者がイチからRTSを作るまでを書き記す日記

3回目・視野内に入ったプレイヤーを感知して突進する敵AI

    public GameObject player;
    private NavMeshAgent agent;

    void Start ()
    {
        player = GameObject.Find("Player");
        agent = GetComponent<NavMeshAgent>();
    }

    void Update ()
    {
        Vector3 forward = transform.TransformDirection(Vector3.forward);
        Vector3 targetDirection = player.transform.position - transform.position;
        float distance = targetDirection.magnitude;
        float angle = Vector3.Angle(forwardtargetDirection);

        if (distance < 4 && angle < 90)
            agent.SetDestination(player.transform.position);
    }

    void OnTriggerEnter(Collider other)
    {
        if(other.tag == "Player")    
        {
            Debug.Log ("hit");
        }
    }

 これだけで探査型AIの出来上がり〜