Exemplo n.º 1
0
    void FixedUpdate()
    {
        //If the core of the capsule is clipping, the capsule is likely being squished.
        if (m_IsActive && m_ControlledCollider.GetCapsuleTransform().IsBeingSquished(m_SquishRadius))
        {
            m_IsBeingSquished = true;
        }
        else
        {
            m_IsBeingSquished = false;
        }
        //Apply ControlledCapsuleCollider transform
        m_CapsuleCollider.height = m_ControlledCollider.GetLength() + m_ControlledCollider.GetRadius() * 2.0f;

        //Detect collisions
        if (m_RigidBody)
        {
            m_RigidBody.MovePosition(transform.position);
        }
    }
Exemplo n.º 2
0
    void Update()
    {
        if (m_CharacterController == null || m_CharacterController.GetCollider() == null)
        {
            Debug.LogError("Sprite animator can't find properly set-up character");
            this.enabled = false;
            return;
        }
        if (Mathf.Abs(m_CharacterController.GetCollider().GetVelocity().x) >= m_MoveSpeedThreshhold)
        {
            m_LastGoodDirection = m_CharacterController.GetCollider().GetVelocity().normalized;
        }
        else if (Mathf.Abs(m_CharacterController.GetInputMovement().x) > 0.0f)
        {
            m_LastGoodDirection = Vector2.right * m_CharacterController.GetInputMovement().x;
        }
        //Rotate and position the capsule according to character velocity and context
        float zRot   = 0.0f;
        float xScale = 1.0f;

        if (m_CurrentAnimationName != "")
        {
            if (m_CharacterController.GetCollider().IsGrounded())
            {
                Vector2 normal = m_CharacterController.GetCollider().GetGroundedInfo().GetNormal();

                xScale = (m_LastGoodDirection.x >= 0.0f) ? 1.0f : -1.0f;
                zRot   = -Mathf.Atan2(normal.x, normal.y) * Mathf.Rad2Deg;
            }
            else if (m_CharacterController.GetCollider().IsPartiallyTouchingWall())
            {
                Vector2 normal = m_CharacterController.GetCollider().GetSideCastInfo().GetSideNormal();
                Vector2 up     = CState.GetDirectionAlongNormal(Vector2.up, normal);
                xScale = ((normal.x <= 0.0f) ? 1.0f : -1.0f);
                zRot   = -Mathf.Atan2(up.x, up.y) * Mathf.Rad2Deg;

                m_LastGoodDirection = -normal;
            }
            else if (m_CharacterController.GetCollider().IsTouchingEdge())
            {
                Vector2 normal = m_CharacterController.GetCollider().GetEdgeCastInfo().GetWallNormal();
                Vector2 up     = CState.GetDirectionAlongNormal(Vector2.up, normal);
                xScale = ((normal.x <= 0.0f) ? 1.0f : -1.0f);
                zRot   = -Mathf.Atan2(up.x, up.y) * Mathf.Rad2Deg;

                m_LastGoodDirection = -normal;
            }
            else
            {
                zRot   = 0.0f;
                xScale = (m_LastGoodDirection.x >= 0.0f) ? 1.0f : -1.0f;
                Vector2 up = m_CharacterController.GetCurrentVisualUp();
                if (up != Vector2.up)
                {
                    zRot = -Mathf.Atan2(up.x, up.y) * Mathf.Rad2Deg;
                }
            }
        }
        if (!m_BlockRotation)
        {
            zRot = Mathf.Lerp(m_LastZRot, zRot, 0.1f);
            m_SpriteTransformHook.transform.rotation = Quaternion.Euler(0.0f, 0.0f, zRot);
        }
        m_SpriteTransformHook.transform.localScale = new Vector3(xScale, 1.0f, 1.0f);
        m_LastZRot = zRot;

        Vector3 startPosition = Vector3.zero;

        //We are being controlled by a different entity, block interpolation
        if (m_CharacterController.IsMovementLocked())
        {
            startPosition = m_CharacterController.transform.position;
        }
        else
        {
            startPosition = GetInterpolatedPosition();
        }
        //Take the bottom of the capsule collider to rotate from
        //Animations themselves might adjust position via localposition
        Vector3 heightAdjustment = m_CapsuleCollider.GetUpDirection() * (-m_CapsuleCollider.GetLength() * 0.5f);

        m_SpriteTransformHook.transform.position = startPosition + heightAdjustment;
        StartAnimation(m_CharacterController.GetCurrentSpriteState());
    }