Skip to content

Commit

Permalink
SameAsFollowObject renamed to SameAsFollowTarget. Also add special AP…
Browse files Browse the repository at this point in the history
…I to get LookAt/Follow pos/rot
  • Loading branch information
glabute committed Nov 29, 2017
1 parent d94af60 commit 858cd12
Show file tree
Hide file tree
Showing 18 changed files with 171 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Base/Editor/Editors/CinemachineHardLockToTargetEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public override void OnInspectorGUI()
MessageType.Warning);
EditorGUI.BeginChangeCheck();
GUI.enabled = false;
EditorGUILayout.LabelField(" ", "Hard Lock has no settings", EditorStyles.miniLabel);
EditorGUILayout.LabelField(" ", "No additional settings", EditorStyles.miniLabel);
GUI.enabled = true;
DrawRemainingPropertiesInInspector();
}
Expand Down
2 changes: 1 addition & 1 deletion Base/Editor/Editors/CinemachineHardLookAtEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public override void OnInspectorGUI()
MessageType.Warning);
EditorGUI.BeginChangeCheck();
GUI.enabled = false;
EditorGUILayout.LabelField(" ", "Hard Look At has no settings", EditorStyles.miniLabel);
EditorGUILayout.LabelField(" ", "No additional settings", EditorStyles.miniLabel);
GUI.enabled = true;
DrawRemainingPropertiesInInspector();
}
Expand Down
3 changes: 1 addition & 2 deletions Base/Editor/Editors/CinemachineHardLookAtEditor.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Base/Editor/Editors/CinemachineOrbitalTransposerEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ static void DrawTransposerGizmos(CinemachineOrbitalTransposer target, GizmoType
CinemachineBrain brain = CinemachineCore.Instance.FindPotentialTargetBrain(target.VirtualCamera);
if (brain != null)
up = brain.DefaultWorldUp;
Vector3 pos = target.FollowTarget.position;
Vector3 pos = target.FollowTargetPosition;

Quaternion orient = target.GetReferenceOrientation(up);
up = orient * Vector3.up;
Expand Down
23 changes: 23 additions & 0 deletions Base/Editor/Editors/CinemachineSameAsFollowTargetEditor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using UnityEditor;
using UnityEngine;

namespace Cinemachine.Editor
{
[CustomEditor(typeof(CinemachineSameAsFollowTarget))]
public sealed class CinemachineSameAsFollowTargetEditor : BaseEditor<CinemachineSameAsFollowTarget>
{
public override void OnInspectorGUI()
{
BeginInspector();
if (Target.FollowTarget == null)
EditorGUILayout.HelpBox(
"Same As Follow Target requires a Follow target. It will set the virtual camera's rotation to be the same as that of the Follow Target.",
MessageType.Warning);
EditorGUI.BeginChangeCheck();
GUI.enabled = false;
EditorGUILayout.LabelField(" ", "No additional settings", EditorStyles.miniLabel);
GUI.enabled = true;
DrawRemainingPropertiesInInspector();
}
}
}
11 changes: 11 additions & 0 deletions Base/Editor/Editors/CinemachineSameAsFollowTargetEditor.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Base/Editor/Editors/CinemachineTransposerEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ static void DrawTransposerGizmos(CinemachineTransposer target, GizmoType selecti
CinemachineBrain brain = CinemachineCore.Instance.FindPotentialTargetBrain(target.VirtualCamera);
if (brain != null)
up = brain.DefaultWorldUp;
Vector3 targetPos = target.FollowTarget.position;
Vector3 targetPos = target.FollowTargetPosition;
Vector3 desiredPos = target.GeTargetCameraPosition(up);
Gizmos.DrawLine(targetPos, desiredPos);
Gizmos.DrawWireSphere(desiredPos, HandleUtility.GetHandleSize(desiredPos) / 20);
//Gizmos.DrawWireSphere(desiredPos, HandleUtility.GetHandleSize(desiredPos) / 20);
Gizmos.color = originalGizmoColour;
}
}
Expand Down
19 changes: 17 additions & 2 deletions Base/Runtime/Behaviours/CinemachineVirtualCamera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -403,13 +403,28 @@ void UpdateComponentPipeline()
m_ComponentPipeline = list.ToArray();
}

private Transform mCachedLookAtTarget;
private CinemachineVirtualCameraBase mCachedLookAtTargetVcam;
private CameraState CalculateNewState(Vector3 worldUp, float deltaTime)
{
// Initialize the camera state, in case the game object got moved in the editor
CameraState state = PullStateFromVirtualCamera(worldUp);

if (LookAt != null)
state.ReferenceLookAt = LookAt.position;
Transform lookAtTarget = LookAt;
if (lookAtTarget != mCachedLookAtTarget)
{
mCachedLookAtTarget = lookAtTarget;
mCachedLookAtTargetVcam = null;
if (lookAtTarget != null)
mCachedLookAtTargetVcam = lookAtTarget.GetComponent<CinemachineVirtualCameraBase>();
}
if (lookAtTarget != null)
{
if (mCachedLookAtTargetVcam != null)
state.ReferenceLookAt = mCachedLookAtTargetVcam.State.FinalPosition;
else
state.ReferenceLookAt = lookAtTarget.position;
}

// Update the state by invoking the component pipeline
CinemachineCore.Stage curStage = CinemachineCore.Stage.Body;
Expand Down
2 changes: 1 addition & 1 deletion Base/Runtime/Components/CinemachineComposer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ protected virtual Vector3 GetLookAtPointAndSetTrackedPoint(Vector3 lookAt)
{
Vector3 pos = lookAt;
if (LookAtTarget != null)
pos += LookAtTarget.transform.rotation * m_TrackedObjectOffset;
pos += LookAtTargetRotation * m_TrackedObjectOffset;

m_Predictor.Smoothing = m_LookaheadSmoothing;
m_Predictor.AddPosition(pos);
Expand Down
2 changes: 1 addition & 1 deletion Base/Runtime/Components/CinemachineFramingTransposer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ public override void MutateCameraState(ref CameraState curState, float deltaTime

//UnityEngine.Profiling.Profiler.BeginSample("CinemachineFramingTransposer.MutateCameraState");
Vector3 camPosWorld = m_PreviousCameraPosition;
curState.ReferenceLookAt = FollowTarget.position;
curState.ReferenceLookAt = FollowTargetPosition;
m_Predictor.Smoothing = m_LookaheadSmoothing;
m_Predictor.AddPosition(curState.ReferenceLookAt);
TrackedPoint = (m_LookaheadTime > 0)
Expand Down
2 changes: 1 addition & 1 deletion Base/Runtime/Components/CinemachineHardLockToTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class CinemachineHardLockToTarget : CinemachineComponentBase
public override void MutateCameraState(ref CameraState curState, float deltaTime)
{
if (IsValid)
curState.RawPosition = FollowTarget.position;
curState.RawPosition = FollowTargetPosition;
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions Base/Runtime/Components/CinemachineOrbitalTransposer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ public override void MutateCameraState(ref CameraState curState, float deltaTime

if (IsValid)
{
mLastTargetPosition = FollowTarget.position;
mLastTargetPosition = FollowTargetPosition;

// Calculate the heading
if (m_BindingMode != BindingMode.SimpleFollowWithWorldUp)
Expand Down Expand Up @@ -385,13 +385,13 @@ private float GetTargetHeading(
switch (m_Heading.m_HeadingDefinition)
{
case Heading.HeadingDefinition.PositionDelta:
velocity = FollowTarget.position - mLastTargetPosition;
velocity = FollowTargetPosition - mLastTargetPosition;
break;
case Heading.HeadingDefinition.Velocity:
velocity = mTargetRigidBody.velocity;
break;
case Heading.HeadingDefinition.TargetForward:
velocity = FollowTarget.forward;
velocity = FollowTargetRotation * Vector3.forward;
break;
default:
case Heading.HeadingDefinition.WorldForward:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ namespace Cinemachine
{
/// <summary>
/// This is a CinemachineComponent in the Aim section of the component pipeline.
/// Its job is to aim the camera hard at the LookAt target.
/// Its job is to match the orientation of the Follow target.
/// </summary>
[DocumentationSorting(DocumentationSortingAttribute.Level.UserRef)]
[AddComponentMenu("")] // Don't display in add component menu
[RequireComponent(typeof(CinemachinePipeline))]
[SaveDuringPlay]
public class CinemachineSameAsFollowObject : CinemachineComponentBase
public class CinemachineSameAsFollowTarget : CinemachineComponentBase
{
/// <summary>True if component is enabled and has a Follow target defined</summary>
public override bool IsValid { get { return enabled && FollowTarget != null; } }
Expand All @@ -19,14 +19,13 @@ public class CinemachineSameAsFollowObject : CinemachineComponentBase
/// Always returns the Aim stage</summary>
public override CinemachineCore.Stage Stage { get { return CinemachineCore.Stage.Aim; } }

/// <summary>Applies the composer rules and orients the camera accordingly</summary>
/// <summary>Orients the camera to match the Follow target's orientation</summary>
/// <param name="curState">The current camera state</param>
/// <param name="deltaTime">Used for calculating damping. If less than
/// zero, then target will snap to the center of the dead zone.</param>
/// <param name="deltaTime">Not used.</param>
public override void MutateCameraState(ref CameraState curState, float deltaTime)
{
if (IsValid)
curState.RawOrientation = FollowTarget.transform.rotation;
curState.RawOrientation = FollowTargetRotation;
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions Base/Runtime/Components/CinemachineTrackedDolly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public override void MutateCameraState(ref CameraState curState, float deltaTime
prevPos = m_Path.GetPathPositionFromDistance(prevPos);
// This works in path units
m_PathPosition = m_Path.FindClosestPoint(
FollowTarget.transform.position,
FollowTargetPosition,
Mathf.FloorToInt(prevPos),
(deltaTime < 0 || m_AutoDolly.m_SearchRadius <= 0)
? -1 : m_AutoDolly.m_SearchRadius,
Expand Down Expand Up @@ -278,11 +278,11 @@ private Quaternion GetTargetOrientationAtPathPoint(Quaternion pathOrientation, V
return Quaternion.LookRotation(pathOrientation * Vector3.forward, up);
case CameraUpMode.FollowTarget:
if (FollowTarget != null)
return FollowTarget.rotation;
return FollowTargetRotation;
break;
case CameraUpMode.FollowTargetNoRoll:
if (FollowTarget != null)
return Quaternion.LookRotation(FollowTarget.rotation * Vector3.forward, up);
return Quaternion.LookRotation(FollowTargetRotation * Vector3.forward, up);
break;
}
return Quaternion.LookRotation(transform.rotation * Vector3.forward, up);
Expand Down
10 changes: 5 additions & 5 deletions Base/Runtime/Components/CinemachineTransposer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ protected void InitPrevFrameStateInfo(
{
m_previousTarget = FollowTarget;
m_targetOrientationOnAssign
= (m_previousTarget == null) ? Quaternion.identity : FollowTarget.rotation;
= (m_previousTarget == null) ? Quaternion.identity : FollowTargetRotation;
}
if (deltaTime < 0)
{
Expand Down Expand Up @@ -197,7 +197,7 @@ protected void TrackTarget(
}
m_PreviousReferenceOrientation = dampedOrientation;

Vector3 targetPosition = FollowTarget.position;
Vector3 targetPosition = FollowTargetPosition;
Vector3 currentPosition = m_PreviousTargetPosition;
Vector3 worldOffset = targetPosition - currentPosition;

Expand Down Expand Up @@ -262,7 +262,7 @@ public Vector3 GeTargetCameraPosition(Vector3 worldUp)
{
if (!IsValid)
return Vector3.zero;
return FollowTarget.position + GetReferenceOrientation(worldUp) * EffectiveOffset;
return FollowTargetPosition + GetReferenceOrientation(worldUp) * EffectiveOffset;
}

/// <summary>State information for damping</summary>
Expand All @@ -276,7 +276,7 @@ public Quaternion GetReferenceOrientation(Vector3 worldUp)
{
if (FollowTarget != null)
{
Quaternion targetOrientation = FollowTarget.rotation;
Quaternion targetOrientation = FollowTargetRotation;
switch (m_BindingMode)
{
case BindingMode.LockToTargetOnAssign:
Expand All @@ -289,7 +289,7 @@ public Quaternion GetReferenceOrientation(Vector3 worldUp)
return targetOrientation;
case BindingMode.SimpleFollowWithWorldUp:
{
Vector3 dir = FollowTarget.position - VcamState.RawPosition;
Vector3 dir = FollowTargetPosition - VcamState.RawPosition;
if (dir.AlmostZero())
break;
return Uppify(Quaternion.LookRotation(dir, worldUp), worldUp);
Expand Down
94 changes: 94 additions & 0 deletions Base/Runtime/Core/CinemachineComponentBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,100 @@ public Transform LookAtTarget
}
}

private Transform mCachedFollowTarget;
private CinemachineVirtualCameraBase mCachedFollowTargetVcam;

/// <summary>Get the position of the Follow target. Special handling: If the Follow target is
/// a VirtualCamera, returns the vcam State's position, not the transform's position</summary>
public Vector3 FollowTargetPosition
{
get
{
Transform target = FollowTarget;
if (target != mCachedFollowTarget)
{
mCachedFollowTargetVcam = null;
mCachedFollowTarget = target;
if (target != null)
mCachedFollowTargetVcam = target.GetComponent<CinemachineVirtualCameraBase>();
}
if (mCachedFollowTargetVcam != null)
return mCachedFollowTargetVcam.State.FinalPosition;
if (target != null)
return target.position;
return Vector3.zero;
}
}

/// <summary>Get the rotation of the Follow target. Special handling: If the Follow target is
/// a VirtualCamera, returns the vcam State's rotation, not the transform's rotation</summary>
public Quaternion FollowTargetRotation
{
get
{
Transform target = FollowTarget;
if (target != mCachedFollowTarget)
{
mCachedFollowTargetVcam = null;
mCachedFollowTarget = target;
if (target != null)
mCachedFollowTargetVcam = target.GetComponent<CinemachineVirtualCameraBase>();
}
if (mCachedFollowTargetVcam != null)
return mCachedFollowTargetVcam.State.FinalOrientation;
if (target != null)
return target.rotation;
return Quaternion.identity;
}
}

private Transform mCachedLookAtTarget;
private CinemachineVirtualCameraBase mCachedLookAtTargetVcam;

/// <summary>Get the position of the LookAt target. Special handling: If the LookAt target is
/// a VirtualCamera, returns the vcam State's position, not the transform's position</summary>
public Vector3 LookAtTargetPosition
{
get
{
Transform target = LookAtTarget;
if (target != mCachedLookAtTarget)
{
mCachedLookAtTargetVcam = null;
mCachedLookAtTarget = target;
if (target != null)
mCachedLookAtTargetVcam = target.GetComponent<CinemachineVirtualCameraBase>();
}
if (mCachedLookAtTargetVcam != null)
return mCachedLookAtTargetVcam.State.FinalPosition;
if (target != null)
return target.position;
return Vector3.zero;
}
}

/// <summary>Get the rotation of the LookAt target. Special handling: If the LookAt target is
/// a VirtualCamera, returns the vcam State's rotation, not the transform's rotation</summary>
public Quaternion LookAtTargetRotation
{
get
{
Transform target = LookAtTarget;
if (target != mCachedLookAtTarget)
{
mCachedLookAtTargetVcam = null;
mCachedLookAtTarget = target;
if (target != null)
mCachedLookAtTargetVcam = target.GetComponent<CinemachineVirtualCameraBase>();
}
if (mCachedLookAtTargetVcam != null)
return mCachedLookAtTargetVcam.State.FinalOrientation;
if (target != null)
return target.rotation;
return Quaternion.identity;
}
}

/// <summary>Returns the owner vcam's CameraState.</summary>
public CameraState VcamState
{
Expand Down
2 changes: 1 addition & 1 deletion Base/Runtime/Core/CinemachineCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public sealed class CinemachineCore
public static readonly int kStreamingVersion = 20170927;

/// <summary>Human-readable Cinemachine Version</summary>
public static readonly string kVersionString = "2.1";
public static readonly string kVersionString = "2.1.10";

/// <summary>
/// Stages in the Cinemachine Component pipeline, used for
Expand Down

0 comments on commit 858cd12

Please sign in to comment.