Skip to content

Commit

Permalink
Smooth camera rotation and translation
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrizov committed Aug 11, 2019
1 parent 23f5641 commit a123689
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 29 deletions.
16 changes: 9 additions & 7 deletions Assets/NaughtyCharacter/Prefabs/CameraRig.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: c13ee35010795cc40a47bd37c09f0215, type: 3}
m_Name:
m_EditorClassIdentifier:
RotationSpeed: 0
RotationSpeed: 25
PositionSmoothDamp: 0.025
Rig: {fileID: 423682}
Pivot: {fileID: 497028}
Camera: {fileID: 4479908057211236910}
Expand Down Expand Up @@ -124,9 +125,10 @@ Camera:
m_ClearFlags: 1
m_BackGroundColor: {r: 0.19215687, g: 0.3019608, b: 0.4745098, a: 0.019607844}
m_projectionMatrixMode: 1
m_GateFitMode: 2
m_FOVAxisMode: 0
m_SensorSize: {x: 36, y: 24}
m_LensShift: {x: 0, y: 0}
m_GateFitMode: 2
m_FocalLength: 50
m_NormalizedViewPortRect:
serializedVersion: 2
Expand Down Expand Up @@ -177,6 +179,11 @@ PrefabInstance:
m_Modification:
m_TransformParent: {fileID: 497028}
m_Modifications:
- target: {fileID: 4232072120738076647, guid: d156b914596d0ae43ba2c7656cef2c47,
type: 3}
propertyPath: m_Name
value: SpringArm
objectReference: {fileID: 0}
- target: {fileID: 4232072120738076646, guid: d156b914596d0ae43ba2c7656cef2c47,
type: 3}
propertyPath: m_LocalPosition.x
Expand Down Expand Up @@ -232,11 +239,6 @@ PrefabInstance:
propertyPath: m_LocalEulerAnglesHint.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4232072120738076647, guid: d156b914596d0ae43ba2c7656cef2c47,
type: 3}
propertyPath: m_Name
value: SpringArm
objectReference: {fileID: 0}
- target: {fileID: 4232072120738076645, guid: d156b914596d0ae43ba2c7656cef2c47,
type: 3}
propertyPath: Camera
Expand Down
5 changes: 0 additions & 5 deletions Assets/NaughtyCharacter/Scripts/Character.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,6 @@ private void FixedUpdate()
Controller.OnCharacterFixedUpdate();
}

private void LateUpdate()
{
Controller.OnCharacterLateUpdate();
}

private void UpdateState()
{
UpdateHorizontalSpeed();
Expand Down
1 change: 0 additions & 1 deletion Assets/NaughtyCharacter/Scripts/Controller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ public abstract class Controller : ScriptableObject
public abstract void Init();
public abstract void OnCharacterUpdate();
public abstract void OnCharacterFixedUpdate();
public abstract void OnCharacterLateUpdate();
}
}
21 changes: 9 additions & 12 deletions Assets/NaughtyCharacter/Scripts/PlayerCamera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,35 @@ public class PlayerCamera : MonoBehaviour
{
[Tooltip("How fast the camera rotates around the pivot. Value <= 0 are interpreted as instant rotation")]
public float RotationSpeed = 0.0f;
public float PositionSmoothDamp = 0.0f;
public Transform Rig; // The root transform of the camera rig
public Transform Pivot; // The point at which the camera pivots around
public Camera Camera;

private Quaternion _pivotTargetLocalRotation; // Controls the X Rotation (Pitch Rotation)
private Quaternion _rigTargetLocalRotation; // Controls the Y Rotation (Yaw Rotation)
private Vector3 _cameraVelocity;

public void SetPosition(Vector3 position)
{
Rig.position = position;
Rig.position = Vector3.SmoothDamp(Rig.position, position, ref _cameraVelocity, PositionSmoothDamp);
}

public void SetControlRotation(Vector2 controlRotation)
{
// Y Rotation (Yaw Rotation)
_rigTargetLocalRotation = Quaternion.Euler(0.0f, controlRotation.y, 0.0f);
Quaternion rigTargetLocalRotation = Quaternion.Euler(0.0f, controlRotation.y, 0.0f);

// X Rotation (Pitch Rotation)
_pivotTargetLocalRotation = Quaternion.Euler(controlRotation.x, 0.0f, 0.0f);
Quaternion pivotTargetLocalRotation = Quaternion.Euler(controlRotation.x, 0.0f, 0.0f);

if (RotationSpeed > 0.0f)
{
Pivot.localRotation =
Quaternion.Slerp(Pivot.localRotation, _pivotTargetLocalRotation, RotationSpeed * Time.deltaTime);

Rig.localRotation =
Quaternion.Slerp(Rig.localRotation, _rigTargetLocalRotation, RotationSpeed * Time.deltaTime);
Rig.localRotation = Quaternion.Slerp(Rig.localRotation, rigTargetLocalRotation, RotationSpeed * Time.deltaTime);
Pivot.localRotation = Quaternion.Slerp(Pivot.localRotation, pivotTargetLocalRotation, RotationSpeed * Time.deltaTime);
}
else
{
Pivot.localRotation = _pivotTargetLocalRotation;
Rig.localRotation = _rigTargetLocalRotation;
Rig.localRotation = rigTargetLocalRotation;
Pivot.localRotation = pivotTargetLocalRotation;
}
}
}
Expand Down
4 changes: 0 additions & 4 deletions Assets/NaughtyCharacter/Scripts/PlayerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ public override void OnCharacterFixedUpdate()
_playerCamera.SetControlRotation(Character.GetControlRotation());
}

public override void OnCharacterLateUpdate()
{
}

private void UpdateControlRotation()
{
Vector2 camInput = _playerInput.CameraInput;
Expand Down

0 comments on commit a123689

Please sign in to comment.