Exemplo n.º 1
0
    void Update()
    {
        var     moveDir = 0;
        Vector3 targetPos;

        switch (State)
        {
        case MorayState.ATTACK:
            moveDir   = 1;
            target    = playerFollowers.GetTarget();
            targetPos = target.position;
            while (segmentTraveled >= 1.0f)
            {
                var        ray = new Ray(transform.position, transform.forward);
                RaycastHit hit;
                if (attackTraveled >= AttackDistance ||
                    (Physics.Raycast(ray.origin, ray.direction, out hit, 1.5f, 1) && hit.transform.tag != "Player"))
                {
                    State = MorayState.RETRACT;
                    return;
                }

                var g       = (GameObject)Instantiate(MoraneSegment, InitialPosition - 2.5f * InitialForward, initialRotation);
                var segment = g.GetComponent <MoraySegment>();
                segment.Moray = this;
                if (TailSegment != null)
                {
                    TailSegment.Tail = segment.transform;
                    segment.Head     = TailSegment.transform;
                }
                else
                {
                    HeadSegment  = segment;
                    segment.Head = transform;
                }
                //segment.InnerUpdate(segmentTraveled / Speed);
                segmentTraveled -= 1.0f;
                TailSegment      = segment;
            }
            break;

        case MorayState.RETRACT:
            targetPos = (HeadSegment == null) ? InitialPosition : HeadSegment.transform.position;
            moveDir   = -1;
            if (HeadSegment == null && Vector3.Distance(transform.position, InitialPosition) < 1.0f)
            {
                transform.position = InitialPosition;
                transform.rotation = initialRotation;
                State = MorayState.IDLE;
            }
            break;

        default:
        case MorayState.IDLE:
            target    = playerFollowers.GetTarget();
            targetPos = target.position;
            if (Vector3.Distance(transform.position, targetPos) < SightDistance &&
                Vector3.Angle(transform.forward, targetPos - transform.position) < SightAngle)
            {
                State           = MorayState.ATTACK;
                attackTraveled  = 0.0f;
                segmentTraveled = 0.8f;
            }
            targetPos = transform.position;
            break;
        }

        var newPosition = Vector3.MoveTowards(transform.position, targetPos, Speed * Time.deltaTime);
        var movement    = newPosition - transform.position;

        attackTraveled     += movement.magnitude; //Only used by attackState
        segmentTraveled    += movement.magnitude; //Only used by attackState
        transform.rotation  = Quaternion.LookRotation(Vector3.RotateTowards(transform.forward, moveDir * movement, 0.4f, 0));
        transform.position += moveDir * transform.forward * movement.magnitude;
    }
Exemplo n.º 2
0
    protected void FixedUpdate()
    {
        switch (state)
        {
        case State.PATROL:
            if ((transform.position - target).magnitude < 0.01f)
            {
                SetPathPoint();
            }
            velocity = pathVelocity;
            if (CheckSight(playerFollowers.GetTarget().position) &&
                Vector3.Distance(playerFollowers.transform.position, initialPosition) < maxChaseDistance - 5)
            {
                state = State.INSIGHT;
            }
            break;

        case State.INSIGHT:
            targetTransform = playerFollowers.GetTarget();
            target          = targetTransform.position + ModelOffset;
            velocity        = Mathf.Lerp(velocity, chaseVelocity, 0.1f);
            if (!CheckSight(target - ModelOffset) || Vector3.Distance(transform.position, initialPosition) > maxChaseDistance)
            {
                state  = State.PATROL;
                target = patrolPath[curPatTarget % patrolPath.Count];
            }
            else if (Vector3.Distance(transform.position, target) < ChargeDistance)
            {
                state = State.CHARGE;
            }
            break;

        case State.OUTOFSIGHT:
            break;

        case State.CHARGE:
            targetTransform = playerFollowers.GetTarget();
            target          = targetTransform.position + ModelOffset;
            velocity        = Mathf.Lerp(velocity, chargeVelocity, 0.1f);
            //Transition to eat through collision
            break;

        case State.EAT:
            velocity = Mathf.Lerp(velocity, chaseVelocity, 0.1f);
            if (timer < 0)
            {
                state = State.INSIGHT;
            }
            break;

        case State.FROZEN:
            return;
        }

        timer -= Time.deltaTime;
        if (animator != null && targetTransform != null)
        {
            float dist = Vector3.Distance(transform.position, targetTransform.position);
            if (dist < 22 && dist > 8)
            {
                animator.SetInteger("Mouth", -1);
            }
            else
            {
                animator.SetInteger("Mouth", 1);
            }
        }

        Vector3 newPosition = Vector3.MoveTowards(transform.position, target, velocity * Time.deltaTime);
        Vector3 movement    = newPosition - transform.position;

        //wiggle.wiggleSpeed = movement.magnitude * 50 + 10;
        transform.rotation  = Quaternion.LookRotation(Vector3.RotateTowards(transform.forward, movement, 0.1f, 0));
        transform.position += transform.forward * movement.magnitude;
    }