コード例 #1
0
ファイル: CasterAI.cs プロジェクト: ElliotHume/LabyrinthianVR
    void FixedUpdate()
    {
        // Get player controller position in worldspace
        playerPos = player.transform.TransformPoint(playerController.center);

        // Check if the target is in sight range
        targetInSightRange = Vector3.Distance(transform.position, playerPos) <= sightRange;

        // Check if target is in attack range
        targetInAttackRange = Vector3.Distance(transform.position, playerPos) <= attackRange;

        // If in sight range, check if the target is obscured
        if (targetInSightRange || targetInAttackRange)
        {
            RaycastHit raycastHit;
            if (Physics.SphereCast(transform.position + transform.up, 0.5f, (playerPos - (transform.position + transform.up)), out raycastHit, 100f, sightBlockingMask))
            {
                // Debug.Log("Can see: "+raycastHit.transform.gameObject);
                canSeeTarget = raycastHit.transform.gameObject.tag == "Player";
            }
        }

        // Set animation variable(s)
        anim.SetBool("Moving", walking);

        // Set combat state
        if (!inCombat && targetInSightRange && canSeeTarget)
        {
            inCombat = true;
        }

        // Detect any spells in the area
        SpellData spellInArea = DetectSpells();

        // Check if the spells in the area will hit me
        bool willGetHit = (spellInArea != null) && spellInArea.WillHitObject(gameObject);

        // if (spellInArea != null) Debug.Log("Will I get hit by the incoming spell: "+willGetHit);

        // Check if waiting
        if (waitTime > 0f && !currentlyAttacking && !blocking && !takingDamage)
        {
            waitTime -= Time.deltaTime;
            // Debug.Log("Waiting...");
        }
        bool waiting = waitTime > 0f;

        // Thinking Logic
        if (health > 0f && willGetHit)
        {
            Block(spellInArea);
        }
        else if (health > 0f && inCombat && !waiting)
        {
            if ((!targetInAttackRange || (targetInAttackRange && !canSeeTarget)) && !currentlyAttacking && !blocking && !takingDamage)
            {
                ChaseTarget();
            }
            else
            {
                AttackTarget();
            }
        }
    }