Example #1
0
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Mouse0) && !m_isDragging)
        {
            m_initialDragPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            if (!CheckMouseValidPos(m_initialDragPos))
            {
                return;
            }
            m_currentBall = GameObject.Instantiate(m_ballPrefab, (Vector3)GetValidMousePosition(m_initialDragPos) + Vector3.back * 2f, Quaternion.identity, this.transform).GetComponent <DragBall>();

            Color ballColor = m_ballColors[Mathf.Min(Random.Range(0, m_ballColors.Length), m_ballColors.Length - 1)];
            m_currentBall.SetData(m_cameraShake, ballColor, this);

            m_isDragging = true;

            m_lineRenderer.enabled        = true;
            m_lineRenderer.material.color = ballColor;
            m_lineRenderer.SetPosition(0, (Vector3)m_initialDragPos + Vector3.back);
            m_lineRenderer.SetPosition(1, (Vector3)m_initialDragPos + Vector3.back);
        }
        if (m_isDragging)
        {
            Vector2 validPos = GetValidMousePosition(Camera.main.ScreenToWorldPoint(Input.mousePosition));
            Vector2 PosDiff  = m_initialDragPos - validPos;
            m_currentBall.transform.position = (Vector3)validPos + Vector3.back * 2f;
            m_lineRenderer.SetPosition(1, (Vector3)validPos + Vector3.back);

            if (Input.GetKeyUp(KeyCode.Mouse0))
            {
                m_isDragging           = false;
                m_lineRenderer.enabled = false;
                if (PosDiff.magnitude > 0.3f)
                {
                    Rigidbody2D rb = m_currentBall.GetComponent <Rigidbody2D>();
                    m_currentBall.LaunchBall((PosDiff) * m_ballVelocityMultiplyer, m_ballGravity);
                }
                else
                {
                    m_currentBall.StartCoroutine("DestroyBall");
                }
            }
        }
    }
Example #2
0
 private void OnDisable()
 {
     m_isDragging           = false;
     m_currentBall          = null;
     m_lineRenderer.enabled = false;
 }