コード例 #1
0
        // Update is called once per frame
        void Update()
        {
            if (_analogInput != null || _analogOutput != null || _dragInput != null)
            {
                Quaternion startRot = _initRot * _dragRot;

                Vector3 up = Vector3.zero;
                if (upAxis == Axis.X)
                {
                    up = Vector3.right;
                }
                else if (upAxis == Axis.Y)
                {
                    up = Vector3.up;
                }
                else if (upAxis == Axis.Z)
                {
                    up = Vector3.forward;
                }
                if (invert)
                {
                    up = -up;
                }

                Vector3 forward = Vector3.zero;
                if (forwardAxis == Axis.X)
                {
                    forward = Vector3.right;
                }
                else if (forwardAxis == Axis.Y)
                {
                    forward = Vector3.up;
                }
                else if (forwardAxis == Axis.Z)
                {
                    forward = Vector3.forward;
                }

                Vector3 to = Vector3.ProjectOnPlane(transform.localRotation * forward, startRot * up);

                float angle = Vector3.Angle(startRot * forward, to);
                if (Vector3.Dot(startRot * up, Vector3.Cross(startRot * forward, to)) < 0f)
                {
                    angle = -angle;
                }

                // if this reactor has an output, set output value as "angle"
                if (_analogOutput != null)
                {
                    //set the mass as an identity value (1 for multiplication)
                    float mass = 1;

                    // set the correction factor as an identity value (1 for multiplication)
                    float correctionFactor = 1;

                    // if the hand is holding an object tagged as "Interactable", account for its mass
                    if (hand.currentAttachedObject && hand.currentAttachedObject.CompareTag("Interactable"))
                    {
                        Debug.Log("Attached object");

                        GameObject attached          = hand.currentAttachedObject;
                        Rigidbody  attachedRigidBody = attached.GetComponent <Rigidbody>();
                        if (attachedRigidBody)
                        {
                            mass = attachedRigidBody.mass;
                            Debug.Log(string.Format("Mass = {0}", correctionFactor));

                            if (mass < MIN_MASS || mass > MAX_MASS)
                            {
                                throw new UnityException(string.Format("Mass {0} is out of [{0},{0}] range!", mass, MIN_MASS, MAX_MASS));
                            }
                        }
                    }
                    else
                    {
                        Debug.Log("No object");
                    }

                    correctionFactor = (mass - MIN_MASS) / (MAX_MASS - MIN_MASS);

                    Debug.Log(string.Format("Correction = {0}", correctionFactor));

                    _analogOutput.output = angle * correctionFactor;
                }

                if (_analogInput != null)
                {
                    float value = _analogInput.input;
                    if (value > 180f)
                    {
                        value -= 360f;
                    }
                    else if (value < -180f)
                    {
                        value += 360f;
                    }

                    transform.Rotate(up, value - angle, Space.Self);
                }

                if (_dragInput != null)
                {
                    DragData dragData = _dragInput.input;
                    if (dragData.isDrag)
                    {
                        Quaternion delta = Quaternion.AngleAxis(dragData.delta, up);
                        transform.localRotation *= delta;
                        _dragRot *= delta;
                    }
                }
            }
        }
コード例 #2
0
        // Update is called once per frame
        void Update()
        {
            if (_analogInput != null || _analogOutput != null || _dragInput != null)
            {
                Quaternion startRot = _initRot * _dragRot;

                Vector3 up = Vector3.zero;
                if (upAxis == Axis.X)
                {
                    up = Vector3.right;
                }
                else if (upAxis == Axis.Y)
                {
                    up = Vector3.up;
                }
                else if (upAxis == Axis.Z)
                {
                    up = Vector3.forward;
                }
                if (invert)
                {
                    up = -up;
                }

                Vector3 forward = Vector3.zero;
                if (forwardAxis == Axis.X)
                {
                    forward = Vector3.right;
                }
                else if (forwardAxis == Axis.Y)
                {
                    forward = Vector3.up;
                }
                else if (forwardAxis == Axis.Z)
                {
                    forward = Vector3.forward;
                }

                Vector3 to = Vector3.ProjectOnPlane(transform.localRotation * forward, startRot * up);

                float angle = Vector3.Angle(startRot * forward, to);
                if (Vector3.Dot(startRot * up, Vector3.Cross(startRot * forward, to)) < 0f)
                {
                    angle = -angle;
                }

                if (_analogOutput != null)
                {
                    _analogOutput.output = angle;
                }

                if (_analogInput != null)
                {
                    float value = _analogInput.input;
                    if (value > 180f)
                    {
                        value -= 360f;
                    }
                    else if (value < -180f)
                    {
                        value += 360f;
                    }

                    transform.Rotate(up, value - angle, Space.Self);
                }

                if (_dragInput != null)
                {
                    DragData dragData = _dragInput.input;
                    if (dragData.isDrag)
                    {
                        Quaternion delta = Quaternion.AngleAxis(dragData.delta, up);
                        transform.localRotation *= delta;
                        _dragRot *= delta;
                    }
                }
            }
        }
コード例 #3
0
 public DragData(DragData source)
 {
     isDrag = source.isDrag;
     delta  = source.delta;
     force  = source.force;
 }