Example #1
0
        void Update()
        {
            GetInput();

            if (_waitingForRelease && _clickUp)
            {
                _waitingForRelease = false;
                if (_pickedUp.velocity.y > 0)
                {
                    _pickedUp.velocity = new Vector3(_pickedUp.velocity.x, 0f, _pickedUp.velocity.z);
                }
                _pickedUp.useGravity = true;
                //_pickedUp.freezeRotation = false;
                _pickedUp = null;
                _offset   = Vector3.zero;

                if (_closestColliderBelow)
                {
                    _closestColliderBelow.enabled = false;
                    _closestColliderBelow         = null;
                }
            }

            Ray        ray = _cam.ScreenPointToRay(Input.mousePosition);
            RaycastHit hitInfo;

            Vector3 offsetBase = Vector3.zero;

            if (Physics.Raycast(ray, out hitInfo, _distance, _interactSurfaceMask))
            {
                offsetBase = hitInfo.point;
                if (_pickedUp)
                {
                    float closestColliderY = float.MinValue;
                    if (_closestColliderBelow && _closestColliderBelow._closestTransform)
                    {
                        closestColliderY = _closestColliderBelow._closestTransform.position.y;
                    }

                    Vector3 desiredPos = offsetBase + _offset;
                    desiredPos.y = Mathf.Max(desiredPos.y, closestColliderY + _bufferDistance);
                    Vector3 diff = desiredPos - _pickedUp.position;
                    _pickedUp.velocity = diff.normalized * _velocityCurve.Evaluate(diff.magnitude / _maxVelocityDistance) * _maxVelocity;
                }
            }

            if (Physics.Raycast(ray, out hitInfo, _distance, _interactableMask))
            {
                Selectable i = hitInfo.collider.GetComponentInChildren <Selectable>();
                if (_clickDown && !_waitingForRelease && i && i._canBePickedUp)
                {
                    _waitingForRelease = true;
                    _pickedUp          = hitInfo.collider.GetComponent <Rigidbody>();
                    if (_pickedUp)
                    {
                        _pickedUp.useGravity = false;
                        //_pickedUp.freezeRotation = true;
                    }
                    else
                    {
                        _waitingForRelease = false;
                    }
                    _offset = _pickedUp.position - offsetBase + _pickUpOffset;

                    _closestColliderBelow = hitInfo.collider.GetComponent <ClosestColliderBelow>();
                    if (_closestColliderBelow)
                    {
                        _closestColliderBelow.enabled = true;
                    }

                    Debug.Log("Picked up " + hitInfo.collider.name + ". With offset " + _offset);
                }
            }
        }
        void Update()
        {
            GetInput();

            // Pickup release
            if (_waitingForRelease && _pickupUp)
            {
                _waitingForRelease = false;
                if (_pickedUp.velocity.y > 0)
                {
                    _pickedUp.velocity = new Vector3(_pickedUp.velocity.x, 0f, _pickedUp.velocity.z);
                }
                _pickedUp.useGravity = true;
                _pickedUp            = null;
                _offset = Vector3.zero;

                if (_closestColliderBelow)
                {
                    _closestColliderBelow.enabled = false;
                    _closestColliderBelow         = null;
                }
            }

            Ray        ray = _cam.ScreenPointToRay(Input.mousePosition);
            RaycastHit hitInfo;

            Vector3 offsetBase = Vector3.zero;

            if (Physics.Raycast(ray, out hitInfo, _distance, _selectableSurfaceMask))
            {
                offsetBase = hitInfo.point;
                if (_pickedUp)
                {
                    float closestColliderY = float.MinValue;
                    if (_closestColliderBelow && _closestColliderBelow._closestTransform)
                    {
                        closestColliderY = _closestColliderBelow._closestTransform.position.y;
                    }

                    Vector3 desiredPos = offsetBase + _offset;
                    desiredPos.y = Mathf.Max(desiredPos.y, closestColliderY + _bufferDistance);
                    Vector3 diff = desiredPos - _pickedUp.position;
                    _pickedUp.velocity = diff.normalized * _velocityCurve.Evaluate(diff.magnitude / _maxVelocityDistance) * _maxVelocity;
                }
            }

            if (Physics.Raycast(ray, out hitInfo, _distance, _selectableMask))
            {
                Selectable sel = hitInfo.collider.GetComponentInChildren <Selectable>();
                if (sel)
                {
                    // Pickup
                    if (_pickupDown && !_waitingForRelease && sel._canBePickedUp)
                    {
                        _waitingForRelease = true;
                        _pickedUp          = hitInfo.collider.GetComponent <Rigidbody>();
                        if (_pickedUp)
                        {
                            _pickedUp.useGravity = false;
                        }
                        else
                        {
                            _waitingForRelease = false;
                        }
                        _offset = _pickedUp.position - offsetBase + _pickUpOffset;

                        _closestColliderBelow = hitInfo.collider.GetComponent <ClosestColliderBelow>();
                        if (_closestColliderBelow)
                        {
                            _closestColliderBelow.enabled = true;
                        }

                        //Debug.Log("Picked up " + hitInfo.collider.name + ". With offset " + _offset);
                    }

                    // Select
                    if (_selectDown)
                    {
                        Selectable selectable = hitInfo.collider.GetComponentInChildren <Selectable>();
                        if (selectable.IsSelected())
                        {
                            _selected.Remove(selectable);
                            _selectedTransforms.Remove(selectable.transform);
                            selectable.Deselect();
                        }
                        else
                        {
                            _selected.Add(selectable);
                            _selectedTransforms.Add(selectable.transform);
                            selectable.Select();
                        }
                    }
                }
            }

            if (_scrollWheel != 0f)
            {
                RotateSelected(_scrollWheel * 10f);
            }
        }