コード例 #1
0
    /// <summary>
    /// Update is called once per frame.
    /// </summary>
    void Update()
    {
        ProcessAllTriggers();

        //Start Event
        if (!_actionTriggered && SupportedTriggers[0].Success)
        {
            _actionTriggered = true;

            ((RotationTrigger)SupportedTriggers[1]).Restart = true;
            return;
        }

        //Stop Event
        if (_actionTriggered && SupportedTriggers[2].Success)
        {
            _actionTriggered = false;
        }

        if (!_actionTriggered)
        {
            return;
        }

        RotationTrigger trgr = (RotationTrigger)SupportedTriggers[1];

        if (trgr.Success)
        {
            if (RotateDumpingFactor == 0)
            {
                Debug.LogError("RotateDumpingFactor must not be zero. Changing it to 1");
                RotateDumpingFactor = 1;
            }

            _xRot = trgr.Pitch / RotateDumpingFactor;
            _yRot = -trgr.Yaw / RotateDumpingFactor;
            _zRot = -trgr.Roll / RotateDumpingFactor;

            //Keep it relative - for each axis, if rotation angle is 0 or if this is the first "frame" after a rotation angle is detected, save the data for next frame
            if (!ContinuousRotation)
            {
                if (_xRot == 0 || _lastX == 0)
                {
                    _lastX = _xRot;
                }
                else
                {
                    _xRot = _xRot - _lastX;
                }

                if (_yRot == 0 || _lastY == 0)
                {
                    _lastY = _yRot;
                }
                else
                {
                    _yRot = _yRot - _lastY;
                }

                if (_zRot == 0 || _lastZ == 0)
                {
                    _lastZ = _zRot;
                }
                else
                {
                    _zRot = _zRot - _lastZ;
                }
            }

            if (ContinuousRotation && ContinuousRotationSpeed != 0)
            {
                _xRot = Mathf.Sign(_xRot) * ContinuousRotationSpeed;
                _yRot = Mathf.Sign(_yRot) * ContinuousRotationSpeed;
                _zRot = Mathf.Sign(_zRot) * ContinuousRotationSpeed;
            }

            //Make sure we didn't pass the Maximum Rotation Angles
            _xRot = ((_xRot + _rotationAngleX) > Constraints.XRotation.MaxPos) ? (Constraints.XRotation.MaxPos - _rotationAngleX): _xRot;
            _yRot = ((_yRot + _rotationAngleY) > Constraints.YRotation.MaxPos) ? (Constraints.YRotation.MaxPos - _rotationAngleY): _yRot;
            _zRot = ((_zRot + _rotationAngleZ) > Constraints.ZRotation.MaxPos) ? (Constraints.ZRotation.MaxPos - _rotationAngleZ): _zRot;

            _xRot = ((_xRot + _rotationAngleX) < Constraints.XRotation.MaxNeg) ? (Constraints.XRotation.MaxNeg - _rotationAngleX): _xRot;
            _yRot = ((_yRot + _rotationAngleY) < Constraints.YRotation.MaxNeg) ? (Constraints.YRotation.MaxNeg - _rotationAngleY): _yRot;
            _zRot = ((_zRot + _rotationAngleZ) < Constraints.ZRotation.MaxNeg) ? (Constraints.ZRotation.MaxNeg - _rotationAngleZ): _zRot;

            //Enable / Disable Axis
            _xRot = Constraints.Freeze.X ? 0f : _xRot;
            _yRot = Constraints.Freeze.Y ? 0f : _yRot;
            _zRot = Constraints.Freeze.Z ? 0f : _zRot;


            if (SmoothingFactor > 0)
            {
                Vector3 vec = new Vector3(_xRot, _yRot, _zRot);

                vec   = _rotationSmoothingUtility.ProcessSmoothing(SmoothingType, SmoothingFactor, vec);
                _xRot = vec.x;
                _yRot = vec.y;
                _zRot = vec.z;
            }

            //Set Axis && Rotate
            if (RotateAround == RotateAroundE.SelfAxis)
            {
                upAxis      = Vector3.up;
                forwardAxis = Vector3.forward;

                this.gameObject.transform.Rotate(upAxis, _yRot, Space.Self);
                this.gameObject.transform.Rotate(forwardAxis, _zRot, Space.Self);
                this.gameObject.transform.Rotate(forwardAxis, _xRot, Space.Self);
            }
            else if (RotateAround == RotateAroundE.WorldAxis)
            {
                upAxis      = Vector3.up;
                forwardAxis = Vector3.forward;

                this.gameObject.transform.Rotate(upAxis, _yRot, Space.World);
                this.gameObject.transform.Rotate(forwardAxis, _zRot, Space.World);
                this.gameObject.transform.Rotate(forwardAxis, _xRot, Space.World);
            }
            else if (RotateAround == RotateAroundE.GameObjectAxis)
            {
                upAxis      = RefGameObject.transform.up;
                forwardAxis = RefGameObject.transform.forward;

                Vector3 point = this.transform.localPosition;
                this.gameObject.transform.RotateAround(point, upAxis, _yRot);
                this.gameObject.transform.RotateAround(point, forwardAxis, _zRot);
                this.gameObject.transform.RotateAround(point, forwardAxis, _xRot);
            }
            else if (RotateAround == RotateAroundE.Gameobject)
            {
                upAxis      = RefGameObject.transform.up;
                forwardAxis = RefGameObject.transform.forward;

                Vector3 point = RefGameObject.transform.position;
                this.gameObject.transform.RotateAround(point, upAxis, _yRot);
                this.gameObject.transform.RotateAround(point, forwardAxis, _zRot);
                this.gameObject.transform.RotateAround(point, forwardAxis, _xRot);
            }

            //Update new rotation angle
            _rotationAngleX = _rotationAngleX + _xRot;
            _rotationAngleY = _rotationAngleY + _yRot;
            _rotationAngleZ = _rotationAngleZ + _zRot;

            //Enable Full Rotations
            if (_rotationAngleX >= 360f)
            {
                _rotationAngleX = _rotationAngleX - 360f;
            }
            ;
            if (_rotationAngleY >= 360f)
            {
                _rotationAngleY = _rotationAngleY - 360f;
            }
            ;
            if (_rotationAngleZ >= 360f)
            {
                _rotationAngleZ = _rotationAngleZ - 360f;
            }
            ;

            //Enable Full Rotations
            if (_rotationAngleX <= -360f)
            {
                _rotationAngleX = _rotationAngleX + 360f;
            }
            ;
            if (_rotationAngleY <= -360f)
            {
                _rotationAngleY = _rotationAngleY + 360f;
            }
            ;
            if (_rotationAngleZ <= -360f)
            {
                _rotationAngleZ = _rotationAngleZ + 360f;
            }
            ;

            //Update last rotation
            _lastX = trgr.Pitch / RotateDumpingFactor;
            _lastY = -trgr.Yaw / RotateDumpingFactor;
            _lastZ = -trgr.Roll / RotateDumpingFactor;
        }
    }
コード例 #2
0
ファイル: RotationAction.cs プロジェクト: rvnth/TheOmnipotent
    void Update()
    {
        ProcessAllTriggers();

        //Start Event
        //Debug.Log (cursor.mode);
        if (!_actionTriggered && SupportedTriggers[0].Success && cursor.mode == cursor_handle.MODE.DEFAULT)
        {
            _actionTriggered = true;
            cursor.mode      = cursor_handle.MODE.ROTATE;
            ((RotationTrigger)SupportedTriggers[1]).Restart = true;
            return;
        }

        //Stop Event
        if (_actionTriggered && SupportedTriggers[2].Success)
        {
            cursor.mode      = cursor_handle.MODE.DEFAULT;
            _actionTriggered = false;
        }

        if (!_actionTriggered)
        {
            return;
        }

        RotationTrigger trgr = (RotationTrigger)SupportedTriggers[1];

        if (trgr.Success)
        {
            if (RotateDumpingFactor == 0)
            {
                Debug.LogError("RotateDumpingFactor must not be zero. Changing it to 1");
                RotateDumpingFactor = 1;
            }

            _xRot = trgr.Pitch / RotateDumpingFactor;
            _yRot = -trgr.Yaw / RotateDumpingFactor;
            _zRot = -trgr.Roll / RotateDumpingFactor;

            //Keep it relative - for each axis, if rotation angle is 0 or if this is the first "frame" after a rotation angle is detected, save the data for next frame
            if (!ContinuousRotation)
            {
                if (_xRot == 0 || _lastX == 0)
                {
                    _lastX = _xRot;
                }
                else
                {
                    _xRot = _xRot - _lastX;
                }

                if (_yRot == 0 || _lastY == 0)
                {
                    _lastY = _yRot;
                }
                else
                {
                    _yRot = _yRot - _lastY;
                }

                if (_zRot == 0 || _lastZ == 0)
                {
                    _lastZ = _zRot;
                }
                else
                {
                    _zRot = _zRot - _lastZ;
                }
            }

            if (ContinuousRotation && ContinuousRotationSpeed != 0)
            {
                _xRot = Mathf.Sign(_xRot) * ContinuousRotationSpeed;
                _yRot = Mathf.Sign(_yRot) * ContinuousRotationSpeed;
                _zRot = Mathf.Sign(_zRot) * ContinuousRotationSpeed;
            }

            //Make sure we didn't pass the Maximum Rotation Angles
            _xRot = ((_xRot + _rotationAngleX) > Constraints.XRotation.MaxPos) ? (Constraints.XRotation.MaxPos - _rotationAngleX): _xRot;
            _yRot = ((_yRot + _rotationAngleY) > Constraints.YRotation.MaxPos) ? (Constraints.YRotation.MaxPos - _rotationAngleY): _yRot;
            _zRot = ((_zRot + _rotationAngleZ) > Constraints.ZRotation.MaxPos) ? (Constraints.ZRotation.MaxPos - _rotationAngleZ): _zRot;

            _xRot = ((_xRot + _rotationAngleX) < Constraints.XRotation.MaxNeg) ? (Constraints.XRotation.MaxNeg - _rotationAngleX): _xRot;
            _yRot = ((_yRot + _rotationAngleY) < Constraints.YRotation.MaxNeg) ? (Constraints.YRotation.MaxNeg - _rotationAngleY): _yRot;
            _zRot = ((_zRot + _rotationAngleZ) < Constraints.ZRotation.MaxNeg) ? (Constraints.ZRotation.MaxNeg - _rotationAngleZ): _zRot;

            //Enable / Disable Axis
            _xRot = Constraints.Freeze.X ? 0f : _xRot;
            _yRot = Constraints.Freeze.Y ? 0f : _yRot;
            _zRot = Constraints.Freeze.Z ? 0f : _zRot;


            if (SmoothingFactor > 0)
            {
                Vector3 vec = new Vector3(_xRot, _yRot, _zRot);

                vec   = _rotationSmoothingUtility.ProcessSmoothing(SmoothingType, SmoothingFactor, vec);
                _xRot = vec.x;
                _yRot = vec.y;
                _zRot = vec.z;
            }

            //Set Axis && Rotate
            if (RotateAround == RotateAroundE.SelfAxis)
            {
                upAxis      = Vector3.up;
                forwardAxis = Vector3.forward;

                this.gameObject.transform.Rotate(upAxis, _yRot, Space.Self);
                this.gameObject.transform.Rotate(forwardAxis, _zRot, Space.Self);
                this.gameObject.transform.Rotate(forwardAxis, _xRot, Space.Self);
            }
            else if (RotateAround == RotateAroundE.WorldAxis)
            {
                upAxis      = Vector3.up;
                forwardAxis = Vector3.forward;

                this.gameObject.transform.Rotate(upAxis, _yRot, Space.World);
                this.gameObject.transform.Rotate(forwardAxis, _zRot, Space.World);
                this.gameObject.transform.Rotate(forwardAxis, _xRot, Space.World);
            }
            else if (RotateAround == RotateAroundE.GameObjectAxis)
            {
                upAxis      = RefGameObject.transform.up;
                forwardAxis = RefGameObject.transform.forward;

                Vector3 point = this.transform.localPosition;
                this.gameObject.transform.RotateAround(point, upAxis, _yRot);
                this.gameObject.transform.RotateAround(point, forwardAxis, _zRot);
                this.gameObject.transform.RotateAround(point, forwardAxis, _xRot);
            }
            else if (RotateAround == RotateAroundE.Gameobject)
            {
                upAxis      = RefGameObject.transform.up;
                forwardAxis = RefGameObject.transform.forward;

                Vector3 point = RefGameObject.transform.position;
                this.gameObject.transform.RotateAround(point, upAxis, _yRot);
                this.gameObject.transform.RotateAround(point, forwardAxis, _zRot);
                this.gameObject.transform.RotateAround(point, forwardAxis, _xRot);
            }
            else if (RotateAround == RotateAroundE.Point)
            {
                upAxis      = Vector3.up;
                forwardAxis = Vector3.forward;
                Vector3 screenCenter = new Vector3(Screen.width / 2, Screen.height / 2);

                Ray ray = Camera.main.ScreenPointToRay(screenCenter);

                RaycastHit hit;
                Debug.Log(_xRot);
                Debug.Log(_yRot);
                Debug.Log(_zRot);
                if (Physics.Raycast(ray, out hit))
                {
                    Debug.DrawLine(ray.origin, hit.point);

                    Vector3 point = hit.point;
                    this.gameObject.transform.RotateAround(point, upAxis, _yRot);
//					this.gameObject.transform.RotateAround(point, forwardAxis, _zRot);
//					this.gameObject.transform.RotateAround(point, forwardAxis, _xRot);
//
//					Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - mouseOrigin);
//					rigidbody.transform.RotateAround(hit.point, Vector3.up, pos.x*turnSpeed);
                }
            }

            //Update new rotation angle
            _rotationAngleX = _rotationAngleX + _xRot;
            _rotationAngleY = _rotationAngleY + _yRot;
            _rotationAngleZ = _rotationAngleZ + _zRot;

            //Enable Full Rotations
            if (_rotationAngleX >= 360f)
            {
                _rotationAngleX = _rotationAngleX - 360f;
            }
            ;
            if (_rotationAngleY >= 360f)
            {
                _rotationAngleY = _rotationAngleY - 360f;
            }
            ;
            if (_rotationAngleZ >= 360f)
            {
                _rotationAngleZ = _rotationAngleZ - 360f;
            }
            ;

            //Enable Full Rotations
            if (_rotationAngleX <= -360f)
            {
                _rotationAngleX = _rotationAngleX + 360f;
            }
            ;
            if (_rotationAngleY <= -360f)
            {
                _rotationAngleY = _rotationAngleY + 360f;
            }
            ;
            if (_rotationAngleZ <= -360f)
            {
                _rotationAngleZ = _rotationAngleZ + 360f;
            }
            ;

            //Update last rotation
            _lastX = trgr.Pitch / RotateDumpingFactor;
            _lastY = -trgr.Yaw / RotateDumpingFactor;
            _lastZ = -trgr.Roll / RotateDumpingFactor;
        }
    }