Example #1
0
    void MobileControls()
    {
        if (joystick.touchPhase() == TouchPhase.Moved)
        {
            PlayTractorBeamSound();
            RaycastHit2D hit = Physics2D.Raycast(transform.position, joystick.inputValue(), _tractorlength);
            if (_tractorlength < MAX_TRACTOR_LENGTH && !_hitDebris)
            {
                //Debug.DrawRay(transform.position, _worldPos - originalTouch, Color.red);
                _tractorlength++;
            }
            if (!_hitDebris && hit)
            {
                //handles initial tractor beam connection
                TractorConnects(hit);
            }
            if (_hitDebris && !objectScript.isTractored)
            {
                TractorReleases();
            }
            else if (_hitDebris)
            {
                //move debris in the direction that the joystick is going
                if (!hitMyself)
                {
                    _tractorStick.rigidbody.velocity = (joystick.inputValue() * PULL_SPEED / objectScript.objectSize) + GetComponent <Rigidbody2D>().velocity;
                }
                else
                {
                    _tractorStick.rigidbody.velocity = (joystick.inputValue() * PULL_SPEED / objectScript.objectSize);
                }

                if ((Vector2.Distance(_tractorStick.transform.position, transform.position) - (_tractorStick.collider.GetComponent <CircleCollider2D>().radius *_tractorStick.transform.localScale.x)) > MAX_TRACTOR_PUSH)
                {
                    _tractorStick.transform.position = transform.position + (_tractorStick.transform.position - transform.position).normalized * (MAX_TRACTOR_PUSH + (_tractorStick.collider.GetComponent <CircleCollider2D>().radius *_tractorStick.transform.localScale.x));
                }
            }
        }
        else if (joystick.touchPhase() == TouchPhase.Ended)
        {
            StopTractorBeamSound();
            //Handles variable reset
            TractorReleases();
        }
    }
Example #2
0
    // Update is called once per frame
    void Update()
    {
        //update tractor beam line renderer
        TractorBeamRender();


#if UNITY_STANDALONE || UNITY_WEBPLAYER || UNITY_EDITOR
        /* Tractor Beam Controls Below
         *    click to send out a tractor beam in that direction
         *    when it connects with debris it will stick to it and
         *    the object will move towards the mouse.
         *    when the object gets to the mouse position it stops
         *    if the mouse button is released before it gets to
         *    the mouse it will maintain its velocity
         *
         * */
        //when left mouse button is clicked and held

        if (Input.GetMouseButton(0))
        {
            //get mouse click in world coordinates
            _MouseClickedPoint = Camera.main.ScreenToWorldPoint(new Vector2(Input.mousePosition.x, Input.mousePosition.y));

            //sends ray out to check if it hits an object when it does it records which object it hit
            RaycastHit2D hit = Physics2D.Raycast(transform.position, _MouseClickedPoint - transform.position, _tractorlength);
            if (_tractorlength < MAX_TRACTOR_LENGTH && !_hitDebris)
            {
                //Debug.DrawLine(transform.position, _MouseClickedPoint, Color.red);
                _tractorlength++;
            }

            //holds first object it hits and keeps it from hitting another object
            if (!_hitDebris && hit)
            {
                _tractorStick = hit;
                if (objectScript = _tractorStick.collider.GetComponent <MoveableObject>())
                {
                    _hitDebris = true;
                }
            }

            //uses the initial object that was hit by the beam
            if (_hitDebris)
            {
                //create a script for the held object


                //if the object has a MoveableObject script, store it and handle physics

                //draw a line to show tractor beam connection
                Debug.DrawLine(transform.position, _tractorStick.transform.position);

                //move debris in direction of mouse with force (pullspeed/objectsize)
                //_tractorStick.rigidbody.AddForce(((_MouseClickedPoint - _tractorStick.rigidbody.transform.position).normalized) * PULL_SPEED / objectScript.objectSize );

                _tractorStick.rigidbody.velocity = Vector2.Lerp(_MouseClickedPoint - _tractorStick.transform.position, _tractorStick.transform.position, Time.deltaTime) * PULL_SPEED / objectScript.objectSize;

                //if the distance between the _mouse clicked point and the object is <1 the object will stop moving
                if (Vector2.Distance(_MouseClickedPoint, _tractorStick.transform.position) < 1)
                {
                    _tractorStick.rigidbody.velocity = Vector2.zero;
                }
            }
        }


        //when the mouse button is released resets all of the necessary variables
        if (Input.GetMouseButtonUp(0))
        {
            //Debug.Log("Click up");
            _hitDebris     = false;
            _tractorlength = 0;
        }
#elif UNITY_IOS || UNITY_ANDROID
        if (joystick.touchPhase() == TouchPhase.Moved)
        {
            RaycastHit2D hit = Physics2D.Raycast(transform.position, joystick.inputValue(), _tractorlength);
            if (_tractorlength < MAX_TRACTOR_LENGTH && !_hitDebris)
            {
                //Debug.DrawRay(transform.position, _worldPos - originalTouch, Color.red);
                _tractorlength++;
            }

            if (!_hitDebris && hit)
            {
                _tractorStick = hit;
                if (objectScript = _tractorStick.collider.GetComponent <MoveableObject>())
                {
                    _hitDebris = true;
                }
            }

            if (_hitDebris)
            {
                //draw a line to show tractor beam connection
                //Debug.DrawLine(transform.position, _tractorStick.transform.position);

                //move debris in the direction that the joystick is going
                _tractorStick.rigidbody.velocity = joystick.inputValue() * PULL_SPEED / objectScript.objectSize;
            }
        }
        else if (joystick.touchPhase() == TouchPhase.Ended)
        {
            _hitDebris     = false;
            _tractorlength = 0;
        }
#endif
    }