예제 #1
0
    // OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
    override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        if (fish.launchingAttack)
        {
            //checking hit
            RaycastHit2D hit2D;
            //if hit a wall, change direction
            hit2D = Cast("Wall");
            if (hit2D)
            {
                direction = -direction;
                PetUtility.FlipTransform(fish.transform);
                passedEnemies.Clear();
            }

            //if hit an enemy, attack enemy
            hit2D = Cast("Ghost");
            if (hit2D)
            {
                Ghost ghost = hit2D.collider.GetComponent <Ghost>();
                if (!passedEnemies.Contains(ghost))
                {
                    ghost.TakeDamage(fish.damage);
                    passedEnemies.Add(ghost);
                }
            }

            //if comeback to origin position, stop launching attack and set the timer for next attack
            if (Mathf.Abs(fish.transform.position.x - start.x) <= fish.rollingSpeed / 29 && (PetUtility.GetSameFloorGhost(fish.transform.position) == null) && rolling)
            {
                fish.GetComponent <Rigidbody2D>().velocity = Vector2.zero;
                //Debug.Log("Back To Start!");
                rolling = false;
                void FinishAttack()
                {
                    fish.launchingAttack = false;
                    stopTime             = Time.time;
                }

                PetUtility.WaitAndDo(fish.rollingGap, FinishAttack);
                return;
            }
            else if (rolling)
            {
                //setting velocity
                Vector2 velocity = fish.GetComponent <Rigidbody2D>().velocity;
                velocity.x = fish.rollingSpeed * direction;
                fish.GetComponent <Rigidbody2D>().velocity = velocity;
            }
        }
        else if (Time.time - fish.rollingGap >= stopTime)
        {
            //start Rolling
            fish.launchingAttack = true;
            rolling = true;
            Init(animator);
        }
    }