コード例 #1
0
ファイル: Unit.cs プロジェクト: ruocheng411/MagicMotion
    public virtual void Update()
    {
        if (!isDead)
        {
            if (state == State.Moving)
            {
                // Moving
                float moveDistance = currentMoveSpeed * Time.deltaTime;
                controller.Move(moveDistance * (moveTargetPoint - transform.position).normalized);
                targetRotation = Quaternion.LookRotation(Vector3.ProjectOnPlane(moveTargetPoint - transform.position, Vector3.up));
                if (HorizontalDistance(moveTargetPoint, transform.position) < moveDistance)
                {
                    state = State.Idle;
                }
            }
            else if (state == State.Pursuing)
            {
                // Pursuing
                float moveDistance = currentMoveSpeed * Time.deltaTime;
                if (HorizontalDistance(moveTargetUnit.transform.position, transform.position) > pursueDistance + moveDistance)
                {
                    controller.Move(moveDistance * (moveTargetUnit.transform.position - transform.position).normalized);
                }
                targetRotation = Quaternion.LookRotation(Vector3.ProjectOnPlane(moveTargetUnit.transform.position - transform.position, Vector3.up));
            }
            transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 10);

            if (attackTimer > 0)
            {
                // Attacking
                attackTimer -= Time.deltaTime;
                float attackMoment = attackPeriod - attackDelay;
                if (attackTimer <= attackMoment && attackTimer + Time.deltaTime > attackMoment)
                {
                    AttackAction(attackTarget);
                }
                if (attackTimer <= 0 && state == State.Attacking)
                {
                    state = State.Idle;
                }
            }

            if (castTimer > 0)
            {
                // Casting
                castTimer -= Time.deltaTime;
                float castMoment = currentSpecialAttack.attackPeriod - currentSpecialAttack.attackDelay;
                if (castTimer <= castMoment && castTimer + Time.deltaTime > castMoment)
                {
                    currentSpecialAttack.AttackAction(attackTarget, this);
                }
                if (castTimer <= 0 && state == State.Casting)
                {
                    state = State.Idle;
                }
            }
        }
        else
        {
            decayTimer -= Time.deltaTime;
            if (decayTimer <= 0)
            {
                Destroy(gameObject);
                return;
            }
        }

        // Gravity
        controller.Move(10 * Vector3.down * Time.deltaTime);
    }
コード例 #2
0
ファイル: Unit.cs プロジェクト: supercontact/MagicMotion
    public virtual void Update()
    {
        if (!isDead)
        {
            if (state == State.Moving)
            {
                // Moving
                float moveDistance = currentMoveSpeed * Time.deltaTime;
                controller.Move(moveDistance * (moveTargetPoint - transform.position).normalized);
                targetRotation = Quaternion.LookRotation(Vector3.ProjectOnPlane(moveTargetPoint - transform.position, Vector3.up));
                if (HorizontalDistance(moveTargetPoint, transform.position) < moveDistance)
                {
                    state = State.Idle;
                }
            }
            else if (state == State.Pursuing)
            {
                // Pursuing
                float moveDistance = currentMoveSpeed * Time.deltaTime;
                if (HorizontalDistance(moveTargetUnit.transform.position, transform.position) > pursueDistance + moveDistance)
                {
                    controller.Move(moveDistance * (moveTargetUnit.transform.position - transform.position).normalized);
                }
                targetRotation = Quaternion.LookRotation(Vector3.ProjectOnPlane(moveTargetUnit.transform.position - transform.position, Vector3.up));
            }
            transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 10);

            // Attacking
            if (attackTimer > 0)
            {
                attackTimer -= Time.deltaTime;
            }
            if (state == State.Attacking)
            {
                float attackMoment = attackPeriod - attackDelay;
                if (attackTimer <= attackMoment && !attacked)
                {
                    AttackAction(attackTarget);
                    attacked = true;
                }
                if (attackTimer <= 0)
                {
                    state = State.Idle;
                }
            }

            // Casting
            if (castTimer > 0)
            {
                castTimer -= Time.deltaTime;
            }
            if (state == State.Casting)
            {
                float castMoment = currentSpecialAttack.lasting ? 0 : currentSpecialAttack.attackPeriod - currentSpecialAttack.attackDelay;
                if (castTimer <= castMoment && !cast)
                {
                    currentSpecialAttack.AttackAction();
                    cast = true;
                }
                if (castTimer <= 0 && !currentSpecialAttack.lasting)
                {
                    currentSpecialAttack.EndAction();
                    state = State.Idle;
                }
            }

            // Stunned
            if (state == State.Stunned)
            {
                interruptTimer -= Time.deltaTime;
                if (interruptTimer <= 0 && !isFlying)
                {
                    state = State.Idle;
                }
            }
        }
        else
        {
            decayTimer -= Time.deltaTime;
            if (decayTimer <= 0)
            {
                Destroy(gameObject);
                return;
            }
        }

        // Gravity
        if (state != State.Controlled)
        {
            if (!controller.isGrounded)
            {
                flyingVelocity += gravity * Time.deltaTime * Vector3.down;
                controller.Move(flyingVelocity * Time.deltaTime);
            }
            else
            {
                if (isFlying)
                {
                    LandAction(flyingVelocity);
                    isFlying = false;
                }
                flyingVelocity = Vector3.zero;
            }
        }
        else
        {
            flyingVelocity = Vector3.zero;
        }
    }