示例#1
0
    private void HandleInput()
    {
        if (Input.GetMouseButtonDown(0))
        {
            //hladame throwable objekt
            activeThrowable = FindThrowable();

            //ak sme ho nasli tak mu zavolame Attach
            if (activeThrowable != null)
            {
                activeThrowable.Attach();

                lastPosition = Camera.main.ScreenToViewportPoint(Input.mousePosition);
                lastTime     = Time.time;
            }
        }
        else if (Input.GetMouseButton(0))
        {
            lastPosition = Camera.main.ScreenToViewportPoint(Input.mousePosition);

            //ked pustime palec, tak sa zavola Dettach objektu throwable ktory mu zapne gravitaciu a spadne naspat nazem
            //zistujeme pohyb v priestore cez x,y poziciu mysi a z poziciu objektu relativne na kameru
            if (activeThrowable != null)
            {
                Vector3 position = Camera.main.ScreenToWorldPoint(Input.mousePosition + Vector3.forward * Mathf.Abs(Camera.main.transform.position.z - activeThrowable.transform.position.z));
                position.z = activeThrowable.transform.position.z;

                Vector3 dir = position - activeThrowable.transform.position;

                float velocityMultiplier = dir.magnitude / Time.deltaTime;

                Vector3 velocity = dir * velocityMultiplier;

                activeThrowable.GetRigidbody().velocity = velocity;


                lastPosition = Camera.main.ScreenToViewportPoint(Input.mousePosition);
                lastTime     = Time.time;
            }
        }
        else if (Input.GetMouseButtonUp(0))
        {
            //ked pustime palec, tak sa zavola Dettach objektu
            if (activeThrowable != null)
            {
                float deltaTime = Time.time - lastTime;
                releasePosition = Camera.main.ScreenToViewportPoint(Input.mousePosition);


                Vector3 position = Camera.main.ScreenToWorldPoint(Input.mousePosition + Vector3.forward * Mathf.Abs(Camera.main.transform.position.z - activeThrowable.transform.position.z));
                position.z = activeThrowable.transform.position.z;

                Vector3 dir = position - activeThrowable.transform.position;

                if (dir.magnitude / deltaTime > minimalSwipeVelocity)
                {
                    Vector3 velocity = dir.normalized * throwMultiplier;

                    velocity.z = zVelocity;

                    activeThrowable.Throw(velocity);
                }

                activeThrowable.Dettach();
                activeThrowable = null;
            }
        }
    }