コード例 #1
0
    public float GetWeight()
    {
        float distance;

        if (type == Type.Transition)
        {
            distance = transform.InverseTransformPoint(target.position).z;
            distance = blendCurve.Evaluate(distance.Remap01(-blendRange, blendRange));
        }
        else if (type == Type.Focus)
        {
            if (colliderType == ColliderType.Box)
            {
                distance = Mathf.Abs(transform.InverseTransformPoint(target.position).z);
            }
            else
            {
                distance = (transform.position - target.position).magnitude;
            }

            distance = blendCurve.Evaluate(distance.Remap01(blendRange, 0f));
        }
        else
        {
            distance = path.FindClosestPoint(target.position, 0, 1, 10);

            distance = blendCurve.Evaluate(distance.Remap01(0f, blendRange));
        }

        return(distance);
    }
コード例 #2
0
        private void Start()
        {
            // get the transform of the main camera
            if (Camera.main != null)
            {
                m_Cam = Camera.main.transform;
            }
            else
            {
                Debug.LogWarning(
                    "Warning: no main camera found. Third person character needs a Camera tagged \"MainCamera\", for camera-relative controls.", gameObject);
                // we use self-relative controls in this case, which probably isn't what the user wants, but hey, we warned them!
            }

            // get the third person character ( this should never be null due to require component )
            m_Character = GetComponent <ThirdPersonCharacter>();

            currentPathPosition = path.FindClosestPoint(transform.position, 0, -1, 5);
        }
コード例 #3
0
    private void FixedUpdate()
    {
        float closestPathPos     = m_PathToFollow.FindClosestPoint(m_Target.position, m_StartingSegment, m_SearchRadius, m_NumOfSegments);
        float distanceFromMaxPos = m_PathToFollow.MaxPos - m_DollyCart.m_Position;

        // Mid point of path based on max length
        float midPoint = m_PathToFollow.MaxPos / 2.0f;

        if (m_DollyCart.m_Position >= closestPathPos + 0.025f)
        {
            m_DollyCart.m_Position -= Time.deltaTime * m_RailSpeed;
        }
        else if (m_DollyCart.m_Position <= closestPathPos - 0.025f)
        {
            m_DollyCart.m_Position += Time.deltaTime * m_RailSpeed;
        }
    }
コード例 #4
0
        /// <summary>Positions the virtual camera according to the transposer rules.</summary>
        /// <param name="curState">The current camera state</param>
        /// <param name="deltaTime">Used for damping.  If less that 0, no damping is done.</param>
        public override void MutateCameraState(ref CameraState curState, float deltaTime)
        {
            // Init previous frame state info
            if (deltaTime < 0)
            {
                m_PreviousPathPosition   = m_PathPosition;
                m_PreviousCameraPosition = curState.RawPosition;
            }

            if (!IsValid)
            {
                return;
            }

            //UnityEngine.Profiling.Profiler.BeginSample("CinemachineTrackedDolly.MutateCameraState");
            // Get the new ideal path base position
            if (m_AutoDolly.m_Enabled && FollowTarget != null)
            {
                float prevPos = m_PreviousPathPosition;
                if (m_PositionUnits == CinemachinePathBase.PositionUnits.Distance)
                {
                    prevPos = m_Path.GetPathPositionFromDistance(prevPos);
                }
                // This works in path units
                m_PathPosition = m_Path.FindClosestPoint(
                    FollowTarget.transform.position,
                    Mathf.FloorToInt(prevPos),
                    (deltaTime < 0 || m_AutoDolly.m_SearchRadius <= 0)
                        ? -1 : m_AutoDolly.m_SearchRadius,
                    m_AutoDolly.m_SearchResolution);
                if (m_PositionUnits == CinemachinePathBase.PositionUnits.Distance)
                {
                    m_PathPosition = m_Path.GetPathDistanceFromPosition(m_PathPosition);
                }

                // Apply the path position offset
                m_PathPosition += m_AutoDolly.m_PositionOffset;
            }
            float newPathPosition = m_PathPosition;

            if (deltaTime >= 0)
            {
                // Normalize previous position to find the shortest path
                float maxUnit = m_Path.MaxUnit(m_PositionUnits);
                if (maxUnit > 0)
                {
                    float prev = m_Path.NormalizeUnit(m_PreviousPathPosition, m_PositionUnits);
                    float next = m_Path.NormalizeUnit(newPathPosition, m_PositionUnits);
                    if (m_Path.Looped && Mathf.Abs(next - prev) > maxUnit / 2)
                    {
                        if (next > prev)
                        {
                            prev += maxUnit;
                        }
                        else
                        {
                            prev -= maxUnit;
                        }
                    }
                    m_PreviousPathPosition = prev;
                    newPathPosition        = next;
                }

                // Apply damping along the path direction
                float offset = m_PreviousPathPosition - newPathPosition;
                offset          = Damper.Damp(offset, m_ZDamping, deltaTime);
                newPathPosition = m_PreviousPathPosition - offset;
            }
            m_PreviousPathPosition = newPathPosition;
            Quaternion newPathOrientation = m_Path.EvaluateOrientationAtUnit(newPathPosition, m_PositionUnits);

            // Apply the offset to get the new camera position
            Vector3 newCameraPos = m_Path.EvaluatePositionAtUnit(newPathPosition, m_PositionUnits);
            Vector3 offsetX      = newPathOrientation * Vector3.right;
            Vector3 offsetY      = newPathOrientation * Vector3.up;
            Vector3 offsetZ      = newPathOrientation * Vector3.forward;

            newCameraPos += m_PathOffset.x * offsetX;
            newCameraPos += m_PathOffset.y * offsetY;
            newCameraPos += m_PathOffset.z * offsetZ;

            // Apply damping to the remaining directions
            if (deltaTime >= 0)
            {
                Vector3 currentCameraPos = m_PreviousCameraPosition;
                Vector3 delta            = (currentCameraPos - newCameraPos);
                Vector3 delta1           = Vector3.Dot(delta, offsetY) * offsetY;
                Vector3 delta0           = delta - delta1;
                delta0       = Damper.Damp(delta0, m_XDamping, deltaTime);
                delta1       = Damper.Damp(delta1, m_YDamping, deltaTime);
                newCameraPos = currentCameraPos - (delta0 + delta1);
            }
            curState.RawPosition = m_PreviousCameraPosition = newCameraPos;

            // Set the orientation and up
            Quaternion newOrientation
                = GetTargetOrientationAtPathPoint(newPathOrientation, curState.ReferenceUp);

            if (deltaTime < 0)
            {
                m_PreviousOrientation = newOrientation;
            }
            else
            {
                if (deltaTime >= 0)
                {
                    Vector3 relative = (Quaternion.Inverse(m_PreviousOrientation)
                                        * newOrientation).eulerAngles;
                    for (int i = 0; i < 3; ++i)
                    {
                        if (relative[i] > 180)
                        {
                            relative[i] -= 360;
                        }
                    }
                    relative       = Damper.Damp(relative, AngularDamping, deltaTime);
                    newOrientation = m_PreviousOrientation * Quaternion.Euler(relative);
                }
                m_PreviousOrientation = newOrientation;
            }

            curState.RawOrientation = newOrientation;
            curState.ReferenceUp    = curState.RawOrientation * Vector3.up;
            //UnityEngine.Profiling.Profiler.EndSample();
        }
コード例 #5
0
    private void FixedUpdate()
    {
        if (targetInVolume)
        {
            if (type == Type.Transition)
            {
                float distance = transform.InverseTransformPoint(target.position).z;

                distance = blendCurve.Evaluate(distance.Remap01(-blendRange, blendRange));

                Weight = Mathf.SmoothStep(Weight, distance, smoothing);

                mixer.SetWeight(ACamera, 1 - Weight);
                mixer.SetWeight(BCamera, Weight);
            }
            else if (type == Type.Focus)
            {
                float distance;

                if (colliderType == ColliderType.sphere)
                {
                    distance = (transform.position - target.position).magnitude;
                }
                else
                {
                    distance = Mathf.Abs(transform.InverseTransformPoint(target.position).z);

                    distance.Clamped(0f, blendRange);
                }

                distance = blendCurve.Evaluate(distance.Remap01(blendRange, 0f));

                Weight = Mathf.SmoothStep(Weight, distance, smoothing);

                mixer.SetWeight(ACamera, 1 - Weight);
                mixer.SetWeight(BCamera, Weight);
            }
            else if (type == Type.Curve)
            {
                float distance = path.FindClosestPoint(target.position, 0, -1, 10);

                Debug.Log(blendRange);
                distance = blendCurve.Evaluate(distance.Remap01(0f, blendRange));

                Weight = Mathf.SmoothStep(Weight, distance, smoothing);

                mixer.SetWeight(ACamera, 1 - Weight);
                mixer.SetWeight(BCamera, Weight);
            }
            else if (type == Type.Composite)
            {
            }
        }
        else
        {
            if (HasTarget)
            {
                if (type == Type.Transition || type == Type.Curve)
                {
                    Weight = Weight > 0.5f ? Mathf.SmoothStep(Weight, 1, smoothing) : Mathf.SmoothStep(Weight, 0, smoothing);

                    mixer.SetWeight(ACamera, 1 - Weight);
                    mixer.SetWeight(BCamera, Weight);

                    if (Weight < 0.001f)
                    {
                        Weight = 0f;
                        ACamera.transform.SetParent(null, true);
                        mixer.enabled = false;
                        target        = null;
                    }
                    else if (Weight > 0.999f)
                    {
                        Weight = 1f;
                        BCamera.transform.SetParent(null, true);
                        mixer.enabled = false;
                        target        = null;
                    }
                }
                else if (type == Type.Focus)
                {
                    Weight = Mathf.SmoothStep(Weight, 0f, smoothing);

                    mixer.SetWeight(ACamera, 1 - Weight);
                    mixer.SetWeight(BCamera, Weight);

                    if (Weight < 0.01f)
                    {
                        ACamera.transform.SetParent(null, true);

                        mixer.enabled = false;
                        target        = null;
                    }
                }
                else if (type == Type.Composite)
                {
                }
            }
        }
    }