コード例 #1
0
    void Update()
    {
        fwd  = target.position - transform.position;
        dest = transform.position + transform.forward * lookRadius / 1.5f;
        dist = transform.position + transform.forward * lookRadius * 1.7f;
        //agent.velocity = 3.5f;

        float distance  = Vector3.Distance(target.position, dest);
        float distance2 = Vector3.Distance(target.position, transform.position);

        if (!sleeping)
        {
            if (distance2 < 30f)
            {
                if (!sleeping)
                {
                    pcont.CheckEnemies();
                }
            }
            if (distance <= lookRadius)
            {
                //FaceTarget();
                targetHit = Physics.Raycast(transform.position, fwd, out tHit, lookRadius * 1.7f);

                if (targetHit)
                {
                    if (tHit.transform == target.transform)
                    {
                        //agent.SetDestination(target.position);

                        redEye.AttackEye();
                        isAttacking = true;
                        agent.speed = 7f;
                        xSpeed      = 1f;
                        if (momDino != null)
                        {
                            momDino.SetDestination(target);
                        }
                        else
                        {
                            patrol.SetAttack();
                            patrol.AttackNextPoint(target.position);
                            SetDestination(target);
                        }
                    }
                }

                if (distance2 <= agent.stoppingDistance)
                {
                    if (targetStats != null)
                    {
                        animator.SetBool("attack", true);
                        combat.Attack(targetStats);
                    }

                    FaceTarget();
                }
                else
                {
                    animator.SetBool("attack", false);
                }
            }
            else
            {
                if (isAttacking)
                {
                    patrol.NotAttack();
                    redEye.NotAttackEye();
                    isAttacking = false;
                }
                agent.speed = 1.28f;
                xSpeed      = 0.5f;
            }
            // Debug.Log("vel " + agent.velocity.magnitude);
            float speedPercent = agent.velocity.magnitude / agent.speed * xSpeed;
            animator.SetFloat("speedPercent", speedPercent, locomotionAnimationSmoothTime, Time.deltaTime);
        }
        else
        {
            sleepTime  += .01f;
            isAttacking = false;
            animator.SetBool("attack", false);
            animator.SetBool("sleeping", true);

            if (sleepTime > timeToSleep)
            {
                animator.SetBool("sleeping", false);
                sleeping  = false;
                sleepTime = 0;
                myStats.Born();
                agent.isStopped = false;
            }
        }
    }