Skip to content

Commit

Permalink
Bug Fixes & Default Settings Values
Browse files Browse the repository at this point in the history
- Fixed issue with zooming on Windows. Scroll value is multiplied by 100.
- Fixed issue with cancelling the move to position routine.
- Set some sensible default values for the camera settings.
- Moved ValueRange and RolloffState nested classes to their own script file.
  • Loading branch information
juniordiscart committed Aug 10, 2019
1 parent 7180666 commit c23d0f1
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 133 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/// <summary>
/// Class to keep track of the current rolloff state of a value. It is controlled by time and a curve.
/// </summary>
namespace ImpossibleOdds.TacticalCamera
{
using UnityEngine;

public class RolloffState
{
private float time;
private float rolloffTime;
private float rolloffValue;
private AnimationCurve rolloffCurve = AnimationCurve.Linear(0f, 1f, 1f, 0f);

public float Time
{
get { return time; }
set { time = value; }
}

public float Value
{
get
{
float t = Mathf.InverseLerp(rolloffTime, 0f, time);
return rolloffCurve.Evaluate(t) * rolloffValue;
}
}

public float RolloffTime
{
get { return rolloffTime; }
set
{
if (value <= 0f)
{
throw new System.ArgumentOutOfRangeException("The rolloff time should be a value larger than 0.");
}

rolloffTime = value;
}
}

public float RolloffValue
{
get { return rolloffValue; }
set
{
rolloffValue = value;
time = rolloffTime;
}
}

public AnimationCurve RolloffCurve
{
get { return rolloffCurve; }
set { rolloffCurve = value; }
}

public float Update()
{
if (time == 0f)
{
return 0f;
}

time = Mathf.Clamp(time - UnityEngine.Time.deltaTime, 0f, rolloffTime);
return Value;
}

public void ApplySettings(float rolloffTime, AnimationCurve rolloffCurve)
{
this.rolloffTime = rolloffTime;
this.rolloffCurve = rolloffCurve;
time = 0f;
rolloffValue = 0f;
}
}
}

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

Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
using System.Collections.Generic;
using UnityEngine;

using ValueRange = TacticalCameraSettings.ValueRange;

public class TacticalCamera : MonoBehaviour
{
[SerializeField, Tooltip("Settings that define the camera's behaviour.")]
Expand Down Expand Up @@ -136,11 +134,7 @@ private void OnDisable()
// By setting the time to 0, we basically reset them.
rolloffStates.ForEach(r => r.Time = 0f);

if (moveToPositionHandle != null)
{
StopCoroutine(moveToPositionHandle);
moveToPositionHandle = null;
}
StopMovemeToPositionRoutine();

isOrbiting = false;
orbitPoint = Vector3.zero;
Expand Down Expand Up @@ -240,6 +234,10 @@ private void UpdateMovement()
{
MoveToFocusPoint();
}
else if ((moveToPositionHandle != null) && (moveForwardsState.RolloffTime == moveForwardsState.Time) || (moveSidewaysState.RolloffTime == moveSidewaysState.Time))
{
StopMovemeToPositionRoutine();
}

Vector3 movement = Vector3.zero;

Expand Down Expand Up @@ -372,6 +370,15 @@ private void MoveToFocusPoint()
moveToPositionHandle = StartCoroutine(RoutineMoveToPosition(targetPosition));
}

private void StopMovemeToPositionRoutine()
{
if (moveToPositionHandle != null)
{
StopCoroutine(moveToPositionHandle);
moveToPositionHandle = null;
}
}

private IEnumerator RoutineMoveToPosition(Vector3 endPosition)
{
Vector3 velocity = Vector3.zero;
Expand All @@ -385,80 +392,5 @@ private IEnumerator RoutineMoveToPosition(Vector3 endPosition)

moveToPositionHandle = null;
}

/// <summary>
/// Class to keep track of the current rolloff state of a value. It is controlled by time and a curve.
/// </summary>
private class RolloffState
{
private float time;
private float rolloffTime;
private float rolloffValue;
private AnimationCurve rolloffCurve = AnimationCurve.Linear(0f, 1f, 1f, 0f);

public float Time
{
get { return time; }
set { time = value; }
}

public float Value
{
get
{
float t = Mathf.InverseLerp(rolloffTime, 0f, time);
return rolloffCurve.Evaluate(t) * rolloffValue;
}
}

public float RolloffTime
{
get { return rolloffTime; }
set
{
if (value <= 0f)
{
throw new System.ArgumentOutOfRangeException("The rolloff time should be a value larger than 0.");
}

rolloffTime = value;
}
}

public float RolloffValue
{
get { return rolloffValue; }
set
{
rolloffValue = value;
time = rolloffTime;
}
}

public AnimationCurve RolloffCurve
{
get { return rolloffCurve; }
set { rolloffCurve = value; }
}

public float Update()
{
if (time == 0f)
{
return 0f;
}

time = Mathf.Clamp(time - UnityEngine.Time.deltaTime, 0f, rolloffTime);
return Value;
}

public void ApplySettings(float rolloffTime, AnimationCurve rolloffCurve)
{
this.rolloffTime = rolloffTime;
this.rolloffCurve = rolloffCurve;
time = 0f;
rolloffValue = 0f;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ public float MoveUp
{
float value = Input.mouseScrollDelta.y;

#if UNITY_STANDALONE_WIN
value *= 100f;
#endif

if (invertZoom)
{
value *= -1f;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace ImpossibleOdds.TacticalCamera
{
using System;
using UnityEngine;

[CreateAssetMenu(fileName = "TacticalCameraSettings", menuName = "Impossible Odds/Tactical Camera/Settings")]
Expand All @@ -12,27 +11,27 @@ public class TacticalCameraSettings : ScriptableObject
[SerializeField, Tooltip("Rolloff curve for movement.")]
private AnimationCurve movementRolloff = AnimationCurve.Linear(0f, 1f, 1f, 0f);
[SerializeField, Min(0f), Tooltip("Time for the movement to linger and fade out. (In seconds)")]
private float movementRolloffTime = 0f;
private float movementRolloffTime = 0.2f;
[SerializeField, Min(0f), Range(0f, 1f), Tooltip("Smoothing time when the camera is moving to its focus point.")]
private float moveToTargetSmoothingTime = 0f;
private float moveToTargetSmoothingTime = 0.2f;
[SerializeField, Min(0f), Tooltip("The minimum height above a surface (defined by the interaction mask) and maximum height the camera can go.")]
private ValueRange heightRange = new ValueRange();

[Header("Pivot Settings")]
[SerializeField, Min(0f), Tooltip("Maximum speed at which the camera will pivot around its origin. (In degrees per second)")]
private float maxPivotalSpeed;
private float maxPivotalSpeed = 180f;
[SerializeField, Tooltip("Rolloff curve for pivotal rotation.")]
private AnimationCurve pivotalRolloff = AnimationCurve.Linear(0f, 1f, 1f, 0f);
[SerializeField, Min(0f), Tooltip("Time for the pivotal rolloff to linger and fade out. (In seconds)")]
private float pivotalRolloffTime = 0f;
private float pivotalRolloffTime = 0.2f;

[Header("Orbit Settings")]
[SerializeField, Min(0f), Tooltip("Maximum speed at which the camera will orbit around its focus point. (In degrees per second)")]
private float maxOrbitalSpeed;
private float maxOrbitalSpeed = 90f;
[SerializeField, Tooltip("Rolloff curve for orbital movement.")]
private AnimationCurve orbitalRolloff = AnimationCurve.Linear(0f, 1f, 1f, 0f);
[SerializeField, Min(0f), Tooltip("Time for the orbital rolloff to linger and fade out. (In seconds)")]
private float orbitalRolloffTime = 0f;
private float orbitalRolloffTime = 0.2f;

[Header("Pitch Settings")]
[SerializeField, Tooltip("Range in which the camera can pitch when it is up high. (In degrees)")]
Expand All @@ -46,7 +45,7 @@ public class TacticalCameraSettings : ScriptableObject
[SerializeField, Tooltip("Layers that are to used to interact with the camera movement.")]
private LayerMask interactionMask = 0;
[SerializeField, Tooltip("Maximum length of rays that are used for interacting with the world.")]
private float maxInteractionRayLength = 0;
private float maxInteractionRayLength = 1000f;

/// <summary>
/// Curve that defines the speed of the camera depending on its altitude.
Expand Down Expand Up @@ -191,48 +190,5 @@ public ValueRange HeightRange
get { return heightRange; }
set { heightRange = value; }
}

[Serializable]
public struct ValueRange
{
public float Min
{
get { return Mathf.Min(min, max); }
}

public float Max
{
get { return Mathf.Max(min, max); }
}

[SerializeField]
private float min;
[SerializeField]
private float max;

public static ValueRange Lerp(ValueRange a, ValueRange b, float t)
{
ValueRange result = new ValueRange();
result.min = Mathf.Lerp(a.min, b.min, t);
result.max = Mathf.Lerp(a.max, b.max, t);
return result;
}

public float Clamp(float value)
{
return Mathf.Clamp(value, Min, Max);
}

public void Set(float min, float max)
{
this.min = min;
this.max = max;
}

public override string ToString()
{
return string.Format("Min: {0}, Max: {1}", Min.ToString("0.000"), Max.ToString("0.000"));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
namespace ImpossibleOdds.TacticalCamera
{
using System;
using UnityEngine;

[Serializable]
public struct ValueRange
{
public float Min
{
get { return Mathf.Min(min, max); }
}

public float Max
{
get { return Mathf.Max(min, max); }
}

[SerializeField]
private float min;
[SerializeField]
private float max;

public static ValueRange Lerp(ValueRange a, ValueRange b, float t)
{
ValueRange result = new ValueRange();
result.min = Mathf.Lerp(a.min, b.min, t);
result.max = Mathf.Lerp(a.max, b.max, t);
return result;
}

public float Clamp(float value)
{
return Mathf.Clamp(value, Min, Max);
}

public void Set(float min, float max)
{
this.min = min;
this.max = max;
}

public override string ToString()
{
return string.Format("Min: {0}, Max: {1}", Min.ToString("0.000"), Max.ToString("0.000"));
}
}
}

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

0 comments on commit c23d0f1

Please sign in to comment.