Пример #1
0
    // Update is called once per frame
    void Update()
    {
        float vertical   = Input.GetAxisRaw("Vertical");
        float horizontal = Input.GetAxisRaw("Horizontal");

        animator.SetFloat("vertical", vertical);
        animator.SetFloat("horizontal", horizontal);
        animator.SetBool("walking", Mathf.Abs(horizontal) > 0.01 || Mathf.Abs(vertical) > 0.01);
        animator.SetBool("walkingDiagonal", Mathf.Abs(horizontal) > 0.01 && Mathf.Abs(vertical) > 0.01);


        Vector2 walkingDirection = Vector2.right * horizontal + Vector2.up * vertical;

        rb.velocity = walkingDirection.normalized * walkingSpeed;

        bool ActionE = Input.GetKeyDown(KeyCode.E);
        bool ActionF = Input.GetKeyDown(KeyCode.F);

        if (ActionF)
        {
            if (follower == null)
            {
                Collider2D[] boxes = getLookBoxes();
                foreach (var hit in boxes)
                {
                    Follower f = hit.gameObject.GetComponent <Follower>();
                    if (f != null)
                    {
                        follower = f;
                        follower.followMe(transform);
                        break;
                    }
                }
            }
            else
            {
                follower.unfollow();
                follower = null;
            }
        }

        if (ActionE)
        {
            Collider2D[] boxes = getLookBoxes();
            foreach (var hit in boxes)
            {
                BuildingAnimalController f = hit.gameObject.GetComponent <BuildingAnimalController>();
                if (f != null && follower != null)
                {
                    if (f.addAnimal(follower.GetComponentInParent <Animal>()))
                    {
                        follower.unfollow();
                        follower = null;
                        break;
                    }
                }
                if (f != null && follower == null)
                {
                    Animal a = f.getFirstAnimal();
                    follower = a.gameObject.GetComponent <Follower>();
                    if (follower != null)
                    {
                        follower.followMe(transform);
                    }
                }
            }
        }
    }