コード例 #1
0
    // Update is called once per frame
    public override void Update()
    {
        base.Update();

        if (inSpace)
        {
            return;
        }

        // If the enemy and the player have health left...
        if (playerDetected && agent.enabled)
        {
            bumped = false;
            btimer = 0f;

            if (LineOfSight(player.transform))
            {
                if (!sighted)
                {
                    _playerDetectedAudio.Play();
                    sighted = true;
                }

                // ... set the destination of the nav mesh agent to the player.
                agent.SetDestination(player.transform.position);
                lastPlayerSighting = player.transform.position;
                //			Vector3 distance = (player.transform.position - transform.position);

                //			nav.nextPosition = (nav.speed*Time.deltaTime)*distance.normalized;
                reaction.Exclaim();
            }
            else
            {
                // ... disable the nav mesh agent.
                Vector3 distanceToLastSighting = transform.position - lastPlayerSighting;
                float   distance = distanceToLastSighting.magnitude;
                if (distance >= wanderRadius)
                {
                    agent.SetDestination(lastPlayerSighting);
                }
                else
                {
                    reaction.Question();
                    wander();
                }
            }
        }
        else
        {
            //If got bumped
            if (bumped)
            {
                agent.enabled = false;
                rb.drag       = 0f;
                btimer       += Time.deltaTime;

                if (btimer >= bumpedTimer)
                {
                    //go back to normal, remove bumped state. wander!
                    rb.drag       = initialDrag;
                    bumped        = false;
                    agent.enabled = true;
                    btimer        = 0;
                    wander();
                }
            }
            else
            {
                wander();
            }
        }

        if (dashed)
        {
            bumped        = true;
            dashed        = false;
            agent.enabled = false;
            rb.drag       = 0f;

            Vector3 pushDir = transform.position - playerSpot;
            pushDir.y = 0f;
            pushDir.Normalize();
            rb.AddForce(pushDir * pushedStrength, ForceMode.Impulse);
        }

        anim.SetFloat("MoveSpeed", agent.velocity.sqrMagnitude);
    }