コード例 #1
0
ファイル: Moveable_Drag.cs プロジェクト: Firgof/metanoia-game
        /**
         * <summary>Applies a drag force on the object, based on the movement of the cursor.</summary>
         * <param name = "force">The force vector to apply</param>
         * <param name = "mousePosition">The position of the mouse</param>
         * <param name = "_distanceToCamera">The distance between the object's centre and the camera</param>
         */
        public override void ApplyDragForce(Vector3 force, Vector3 mousePosition, float _distanceToCamera)
        {
            distanceToCamera = _distanceToCamera;

            // Scale force
            force *= speedFactor * _rigidbody.drag * distanceToCamera * Time.deltaTime;

            // Limit magnitude
            if (force.magnitude > maxSpeed)
            {
                force *= maxSpeed / force.magnitude;
            }

            if (dragMode == DragMode.LockToTrack)
            {
                if (track)
                {
                    track.ApplyDragForce(force, this);
                }
            }
            else
            {
                Vector3 newRot = Vector3.Cross(force, cameraTransform.forward);

                if (dragMode == DragMode.MoveAlongPlane)
                {
                    if (alignMovement == AlignDragMovement.AlignToPlane)
                    {
                        if (plane)
                        {
                            _rigidbody.AddForceAtPosition(Vector3.Cross(newRot, plane.up), transform.position + (plane.up * colliderRadius));
                        }
                        else
                        {
                            ACDebug.LogWarning("No alignment plane assigned to " + this.name);
                        }
                    }
                    else
                    {
                        _rigidbody.AddForceAtPosition(force, transform.position - (cameraTransform.forward * colliderRadius));
                    }
                }
                else if (dragMode == DragMode.RotateOnly)
                {
                    newRot /= Mathf.Sqrt((grabPoint.position - transform.position).magnitude) * 2.4f * rotationFactor;
                    _rigidbody.AddTorque(newRot);

                    if (allowZooming)
                    {
                        UpdateZoom();
                    }
                }
            }
        }
コード例 #2
0
        /**
         * <summary>Applies a drag force on the object, based on the movement of the cursor.</summary>
         * <param name = "force">The force vector to apply</param>
         * <param name = "mousePosition">The position of the mouse</param>
         * <param name = "_distanceToCamera">The distance between the object's centre and the camera</param>
         */
        public override void ApplyDragForce(Vector3 force, Vector3 mousePosition, float _distanceToCamera)
        {
            distanceToCamera = _distanceToCamera;

            // Scale force
            force *= speedFactor * distanceToCamera / 50f;

            // Limit magnitude
            if (force.magnitude > maxSpeed)
            {
                force *= maxSpeed / force.magnitude;
            }

            if (dragMode == DragMode.LockToTrack)
            {
                if (track != null)
                {
                    switch (track.dragMovementCalculation)
                    {
                    case DragMovementCalculation.DragVector:
                        track.ApplyDragForce(force, this);
                        break;

                    case DragMovementCalculation.CursorPosition:
                        float mousePositionAlong = track.GetScreenPointProportionAlong(mousePosition);
                        float totalPositionAlong = mousePositionAlong + screenToWorldOffset;

                        if (track.preventEndToEndJumping)
                        {
                            bool inDeadZone = (totalPositionAlong >= 1f || totalPositionAlong <= 0f);
                            if (endLocked)
                            {
                                if (!inDeadZone)
                                {
                                    endLocked = false;
                                }
                            }
                            else
                            {
                                if (inDeadZone)
                                {
                                    endLocked = true;
                                }
                            }

                            if (track.Loops || !endLocked)
                            {
                                lastFrameTotalCursorPositionAlong = totalPositionAlong;
                            }
                            else
                            {
                                totalPositionAlong = lastFrameTotalCursorPositionAlong;
                            }
                        }

                        track.ApplyAutoForce(totalPositionAlong, speedFactor / 10f, this);
                        break;
                    }
                }
            }
            else
            {
                Vector3 newRot = Vector3.Cross(force, KickStarter.CameraMain.transform.forward);

                if (dragMode == DragMode.MoveAlongPlane)
                {
                    if (alignMovement == AlignDragMovement.AlignToPlane)
                    {
                        if (plane)
                        {
                            _rigidbody.AddForceAtPosition(Vector3.Cross(newRot, plane.up), transform.position + (plane.up * grabDistance));
                        }
                        else
                        {
                            ACDebug.LogWarning("No alignment plane assigned to " + this.name, this);
                        }
                    }
                    else
                    {
                        _rigidbody.AddForceAtPosition(force, transform.position - (KickStarter.CameraMain.transform.forward * grabDistance));
                    }
                }
                else if (dragMode == DragMode.RotateOnly)
                {
                    newRot /= Mathf.Sqrt((grabPoint.position - transform.position).magnitude) * 2.4f * rotationFactor;

                    if (moveWithRigidbody)
                    {
                        _rigidbody.AddTorque(newRot);
                    }
                    else
                    {
                        //transform.Rotate (newRot, Space.World);
                        thisFrameTorque = newRot;
                    }

                    if (allowZooming)
                    {
                        UpdateZoom();
                    }
                }
            }
        }
コード例 #3
0
        public override void UpdateMovement()
        {
            base.UpdateMovement();
            if (dragMode == DragMode.LockToTrack && track)
            {
                track.UpdateDraggable(this);

                if (UsesRigidbody && (_rigidbody.angularVelocity != Vector3.zero || _rigidbody.velocity != Vector3.zero))
                {
                    RunInteraction(true);
                }

                if (IsAutoMoving())
                {
                    if (activeAutoMove.CheckForEnd(this))
                    {
                        StopAutoMove(true);
                    }
                }
                else if (!UsesRigidbody && dragMode == DragMode.LockToTrack && track)
                {
                    if (IsHeld)
                    {
                        heldIntensity = 1f;
                    }
                    else
                    {
                        if (heldIntensity > 0.01f && trackValue > 0f && trackValue < 1f)
                        {
                            switch (track.dragMovementCalculation)
                            {
                            case DragMovementCalculation.DragVector:
                                track.ApplyDragForce(lastFrameForce * heldIntensity, this);
                                break;

                            case DragMovementCalculation.CursorPosition:
                                if (simulatedMass > 0)
                                {
                                    track.ApplyAutoForce(lastFrameTotalPositionAlong, heldIntensity * speedFactor * 0.02f / simulatedMass, this, false);
                                }
                                break;

                            default:
                                break;
                            }
                        }
                        heldIntensity = Mathf.Lerp(heldIntensity, 0f, Time.deltaTime * simulatedMass);
                    }
                }

                if (collideSound && !track.UsesEndColliders)
                {
                    if (trackValue > 0.03f && trackValue < 0.97f)
                    {
                        canPlayCollideSound = true;
                    }
                    else if ((Mathf.Approximately(trackValue, 0f) || (!onlyPlayLowerCollisionSound && Mathf.Approximately(trackValue, 1f))) && canPlayCollideSound)
                    {
                        canPlayCollideSound = false;
                        collideSound.Play(collideSoundClip, false);
                    }
                }
            }
            else if (isHeld)
            {
                if (dragMode == DragMode.RotateOnly && allowZooming && distanceToCamera > 0f)
                {
                    LimitZoom();
                }
            }

            if (moveSoundClip && moveSound)
            {
                if (dragMode == DragMode.LockToTrack && track)
                {
                    PlayMoveSound(track.GetMoveSoundIntensity(trackValue - lastFrameTrackValue));
                }
                else if (_rigidbody)
                {
                    PlayMoveSound(_rigidbody.velocity.magnitude);
                }
            }

            lastFrameTrackValue = trackValue;
        }