Exemplo n.º 1
0
    // called when character is entering this collectable
    public void OnTriggerEnter2D(Collider2D otherCollider)
    {
        // do nothing if not ebabled
        if (!enabled)
        {
            return;
        }

        if (m_playing)
        {
            // update playing state
            float fAnimTime = m_anim.GetCurrentAnimatorStateInfo(0).normalizedTime;
            if (fAnimTime >= 1f)
            {
                m_playing = false;
            }
            else
            {
                // do not allow impulse during play
                return;
            }
        }

        APCharacterController character = otherCollider.GetComponent <APCharacterController>();

        if (character != null)
        {
            m_playing = true;
            m_anim.Play(m_animHash, 0, 0f);

            // launch impulse/jump
            if (m_mode == EImpulseMode.Impulse)
            {
                float   fAngle       = -Mathf.Deg2Rad * m_impulseDirection;
                Vector2 v2ImpulseDir = new Vector2(Mathf.Cos(fAngle), -Mathf.Sin(fAngle));
                v2ImpulseDir = transform.TransformDirection(v2ImpulseDir);

                Vector2 charVel         = character.GetVelocity();
                float   velAlongImpulse = Vector2.Dot(charVel, v2ImpulseDir);
                charVel += v2ImpulseDir * (m_impulsePower - velAlongImpulse);
                character.SetVelocity(charVel);
            }
            else
            {
                character.Jump(m_jumpMinHeight, m_jumpMaxHeight);
            }
        }
    }