Esempio n. 1
0
    // Update is called once per frame
    void Update()
    {
        if (carrying)
        {
            // Calculate the desired velocity
            Vector3 offset           = new Vector3(Mathf.Clamp(transform.position.x - player.transform.position.x, -carryOffset.x, carryOffset.x), carryOffset.y, 0f);
            Vector3 desired_velocity = player.transform.position + offset - transform.position;
            float   distance         = desired_velocity.magnitude;

            // Check the distance to detect whether the character
            // is inside the slowing area
            if (distance < carrySlowDistance)
            {
                // Inside the slowing area
                desired_velocity = Vector3.Normalize(desired_velocity) * carryMaxSpeed * (distance / carrySlowDistance);
            }
            else
            {
                // Outside the slowing area
                desired_velocity = Vector3.Normalize(desired_velocity) * carryMaxSpeed;
            }

            // Set the steering based on this
            Vector3 steering = desired_velocity - carryVelocity;
            carryVelocity       = Truncate(carryVelocity + steering, carryMaxSpeed);
            transform.position += carryVelocity;
        }
        else if (!transitioning)
        {
            sittingVelocity.y -= gravity * Time.deltaTime;
            controller.Move(sittingVelocity);

            if (controller.collisions.above || controller.collisions.below)
            {
                sittingVelocity.y = 0f;
            }

            if (coll.bounds.Intersects(player.GetComponent <Collider2D>().bounds))
            {
                audioSource.Play();
                gameManager.AcquireGem();
                StartCoroutine(TransitionToCarry());
            }
        }
    }