Skip to content

Commit

Permalink
Fix UI camera ortho size not updating; make sounds 3D via spatialBlen…
Browse files Browse the repository at this point in the history
…d param (#53)

* Sync game camera with UI camera

* Many fix

* Update prefabs

* Update readme
  • Loading branch information
nucleartide authored Apr 11, 2023
1 parent ea975f5 commit 7f1a122
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 23 deletions.
2 changes: 0 additions & 2 deletions Assets/Plugins/Jason Tu/StateMachine/StateMachineBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ protected void TransitionTo(StateBehaviour newState)

// Update current state reference.
CurrentState = newState;

Debug.Log("Changed state:" + newState);
}

private void StateBehaviour_OnTransitionTo(object sender, StateBehaviour.TransitionToArgs args)
Expand Down
8 changes: 5 additions & 3 deletions Assets/Prefabs/Camera.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
cinemachineCamera: {fileID: 8746842708071168790}
mainCamera: {fileID: 8746842707825717524}
uiCamera: {fileID: 4906026151096002512}
followedObject: {fileID: 0}
gameInput: {fileID: 0}
zoomScaleFactor: 5
Expand Down Expand Up @@ -113,7 +115,7 @@ Camera:
far clip plane: 1000
field of view: 75
orthographic: 1
orthographic size: 5
orthographic size: 3.5
m_Depth: 0
m_CullingMask:
serializedVersion: 2
Expand Down Expand Up @@ -225,7 +227,7 @@ Camera:
far clip plane: 1000
field of view: 75
orthographic: 1
orthographic size: 3.5
orthographic size: 5.711243
m_Depth: 0
m_CullingMask:
serializedVersion: 2
Expand Down Expand Up @@ -372,7 +374,7 @@ MonoBehaviour:
m_Follow: {fileID: 0}
m_Lens:
FieldOfView: 75
OrthographicSize: 3.5
OrthographicSize: 5.711243
NearClipPlane: 0.3
FarClipPlane: 1000
Dutch: 0
Expand Down
2 changes: 1 addition & 1 deletion Assets/Prefabs/Nearby Resources Manager.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Transform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6215328209013302668}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
Expand Down
1 change: 1 addition & 0 deletions Assets/Prefabs/Player.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -1909,3 +1909,4 @@ MonoBehaviour:
playerAnimationEvents: {fileID: 7873346240423897778}
playerMovingState: {fileID: 7520308135305952835}
playerStateMachine: {fileID: 7520308135305952834}
gameCamera: {fileID: 0}
1 change: 1 addition & 0 deletions Assets/Prefabs/Sound Manager.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ MonoBehaviour:
allTheSounds: {fileID: 11400000, guid: 7e5b8a410b35e46d29063206c5999a5b, type: 2}
player: {fileID: 0}
playerStateMachine: {fileID: 0}
gameCamera: {fileID: 0}
22 changes: 22 additions & 0 deletions Assets/Scenes/Game.unity
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,18 @@ PrefabInstance:
objectReference: {fileID: 1238949281}
m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 594144324abd847ada9e69575d7d3295, type: 3}
--- !u!114 &1595643721 stripped
MonoBehaviour:
m_CorrespondingSourceObject: {fileID: 2367151132960774532, guid: 594144324abd847ada9e69575d7d3295,
type: 3}
m_PrefabInstance: {fileID: 1595643720}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 6b806e76dcb454ee49601d3de58a9b9c, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1001 &448395041739114491
PrefabInstance:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -560,6 +572,11 @@ PrefabInstance:
propertyPath: player
value:
objectReference: {fileID: 1614179778878583233}
- target: {fileID: 1060453973320401705, guid: ad6044324a60f4ec69b7d37fbbb69253,
type: 3}
propertyPath: gameCamera
value:
objectReference: {fileID: 1595643721}
- target: {fileID: 1060453973320401705, guid: ad6044324a60f4ec69b7d37fbbb69253,
type: 3}
propertyPath: playerStateMachine
Expand Down Expand Up @@ -714,6 +731,11 @@ PrefabInstance:
propertyPath: gameInput
value:
objectReference: {fileID: 63202250}
- target: {fileID: 7873346240589132618, guid: b7150f0c756f841548259da7df1bee5e,
type: 3}
propertyPath: gameCamera
value:
objectReference: {fileID: 1595643721}
m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: b7150f0c756f841548259da7df1bee5e, type: 3}
--- !u!1001 &4996571555868802377
Expand Down
24 changes: 22 additions & 2 deletions Assets/Scripts/Behaviours/GameCamera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ public class GameCamera : MonoBehaviour
[NotNull]
private Cinemachine.CinemachineVirtualCamera cinemachineCamera;

[SerializeField]
[NotNull]
private Camera mainCamera;

[SerializeField]
[NotNull]
private Camera uiCamera;

[SerializeField]
[NotNull(IgnorePrefab = true)]
private Transform followedObject;
Expand All @@ -27,6 +35,11 @@ public class GameCamera : MonoBehaviour
[SerializeField]
private float initialOrthoSize = 3f;

public float OrthoSizeInverseLerp
{
get => Mathf.InverseLerp(minOrthoSize, maxOrthoSize, mainCamera.orthographicSize);
}

private void Awake()
{
cinemachineCamera.Follow = followedObject;
Expand All @@ -35,15 +48,22 @@ private void Awake()
private void Start()
{
cinemachineCamera.m_Lens.OrthographicSize = initialOrthoSize;
uiCamera.orthographicSize = initialOrthoSize;
}

private void Update()
{
var zoomDelta = gameInput.GetZoom();
if (zoomDelta != 0f)
cinemachineCamera.m_Lens.OrthographicSize += zoomDelta * Time.deltaTime * -zoomScaleFactor;
{
var delta = zoomDelta * Time.deltaTime * -zoomScaleFactor;
cinemachineCamera.m_Lens.OrthographicSize += delta;
uiCamera.orthographicSize += delta;
}

// Prevent ortho size from becoming too small or large.
cinemachineCamera.m_Lens.OrthographicSize = Mathf.Clamp(cinemachineCamera.m_Lens.OrthographicSize, minOrthoSize, maxOrthoSize);
var orthoSizeClamped = Mathf.Clamp(cinemachineCamera.m_Lens.OrthographicSize, minOrthoSize, maxOrthoSize);
cinemachineCamera.m_Lens.OrthographicSize = orthoSizeClamped;
uiCamera.orthographicSize = orthoSizeClamped;
}
}
12 changes: 8 additions & 4 deletions Assets/Scripts/Behaviours/PlayerFootsteps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public class PlayerFootsteps : MonoBehaviour
[NotNull]
private PlayerStateMachine playerStateMachine;

[SerializeField]
[NotNull(IgnorePrefab = true)]
private GameCamera gameCamera;

private List<AudioSourceHelpers.Section> walkFootsteps = new List<AudioSourceHelpers.Section>
{
new AudioSourceHelpers.Section { Start = 0f, End = .72f },
Expand Down Expand Up @@ -63,12 +67,12 @@ private void PlayWalkSound(GroundType groundType)
if (groundType == GroundType.Gravel)
{
var section = walkFootsteps[UnityEngine.Random.Range(0, walkFootsteps.Count)];
AudioSourceHelpers.PlaySoundInterval(footstepAudioSource, section);
AudioSourceHelpers.PlaySoundInterval(footstepAudioSource, section, gameCamera.OrthoSizeInverseLerp);
}
else if (groundType == GroundType.Rock)
{
var section = stoneFootsteps[UnityEngine.Random.Range(0, stoneFootsteps.Count)];
AudioSourceHelpers.PlaySoundInterval(footstepStoneAudioSource, section);
AudioSourceHelpers.PlaySoundInterval(footstepStoneAudioSource, section, gameCamera.OrthoSizeInverseLerp);
}
}

Expand All @@ -77,12 +81,12 @@ private void PlayRunSound(GroundType groundType)
if (groundType == GroundType.Gravel)
{
var section = runFootsteps[UnityEngine.Random.Range(0, runFootsteps.Count)];
AudioSourceHelpers.PlaySoundInterval(footstepAudioSource, section);
AudioSourceHelpers.PlaySoundInterval(footstepAudioSource, section, gameCamera.OrthoSizeInverseLerp);
}
else if (groundType == GroundType.Rock)
{
var section = stoneFootsteps[UnityEngine.Random.Range(0, stoneFootsteps.Count)];
AudioSourceHelpers.PlaySoundInterval(footstepStoneAudioSource, section, 1.2f);
AudioSourceHelpers.PlaySoundInterval(footstepStoneAudioSource, section, gameCamera.OrthoSizeInverseLerp, 1.2f);
}
}

Expand Down
20 changes: 12 additions & 8 deletions Assets/Scripts/Managers/SoundManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public class SoundManager : MonoBehaviour
[NotNull(IgnorePrefab = true)]
private PlayerStateMachine playerStateMachine;

[SerializeField]
[NotNull(IgnorePrefab = true)]
private GameCamera gameCamera;

private void OnEnable()
{
playerAnimationEvents.OnPickaxeHit += PlayerAnimationEvents_OnPickaxeHit;
Expand All @@ -42,17 +46,17 @@ private void OnDisable()

private void PlayerAnimationEvents_OnPickaxeHit(object sender, EventArgs eventArgs)
{
AudioSourceHelpers.PlayClipAtPoint(allTheSounds.PickaxeHit, player.position, 1f);
AudioSourceHelpers.PlayClipAtPoint(allTheSounds.PickaxeHit, player.position, gameCamera.OrthoSizeInverseLerp, 1f);
}

private void PlayerAnimationEvents_OnPickUp(object sender, EventArgs eventArgs)
{
AudioSourceHelpers.PlayClipAtPoint(allTheSounds.WaterSplash, player.position, .25f, .7f);
AudioSourceHelpers.PlayClipAtPoint(allTheSounds.WaterSplash, player.position, gameCamera.OrthoSizeInverseLerp, .25f, .7f);
}

private void PlayerAnimationEvents_OnPickUpComplete(object sender, EventArgs eventArgs)
{
AudioSourceHelpers.PlayClipAtPoint(allTheSounds.MagicSparkle, player.position, .25f, .7f);
AudioSourceHelpers.PlayClipAtPoint(allTheSounds.MagicSparkle, player.position, gameCamera.OrthoSizeInverseLerp, .25f, .7f);
}

private void PlayerAnimationEvents_OnChopImpact(object sender, EventArgs eventArgs)
Expand All @@ -64,13 +68,13 @@ private void PlayerAnimationEvents_OnChopImpact(object sender, EventArgs eventAr
switch (resourceType)
{
case ResourceConfiguration.ResourceType.MediumWood:
AudioSourceHelpers.PlayIntervalAtPoint(allTheSounds.ChopMedium, allTheSounds.ChopMediumRandomSection, player.position);
AudioSourceHelpers.PlayIntervalAtPoint(allTheSounds.ChopMedium, allTheSounds.ChopMediumRandomSection, player.position, gameCamera.OrthoSizeInverseLerp);
break;
case ResourceConfiguration.ResourceType.ThinWood:
AudioSourceHelpers.PlayIntervalAtPoint(allTheSounds.ChopThin, allTheSounds.ChopThinRandomSection, player.position);
AudioSourceHelpers.PlayIntervalAtPoint(allTheSounds.ChopThin, allTheSounds.ChopThinRandomSection, player.position, gameCamera.OrthoSizeInverseLerp);
break;
case ResourceConfiguration.ResourceType.ThickWood:
AudioSourceHelpers.PlayClipAtPoint(allTheSounds.ChopThicc, player.position, 1f, UnityEngine.Random.Range(.8f, 1.2f));
AudioSourceHelpers.PlayClipAtPoint(allTheSounds.ChopThicc, player.position, gameCamera.OrthoSizeInverseLerp, 1f, UnityEngine.Random.Range(.8f, 1.2f));
break;
default:
throw new System.Exception($"Resource type {resourceType} is unsupported.");
Expand All @@ -80,12 +84,12 @@ private void PlayerAnimationEvents_OnChopImpact(object sender, EventArgs eventAr

private void PlayerStateMachine_OnResourceCollisionEnter(object sender, EventArgs eventArgs)
{
AudioSourceHelpers.PlayClipAtPoint(allTheSounds.BumpIntoCollider, player.position, .7f, 1.2f);
AudioSourceHelpers.PlayClipAtPoint(allTheSounds.BumpIntoCollider, player.position, gameCamera.OrthoSizeInverseLerp, .7f, 1.2f);
}

private void PlayerStateMachine_OnNothingToMine(object sender, EventArgs eventArgs)
{
AudioSourceHelpers.PlayClipAtPoint(allTheSounds.NothingToMine, player.position, 1f);
AudioSourceHelpers.PlayClipAtPoint(allTheSounds.NothingToMine, player.position, gameCamera.OrthoSizeInverseLerp, 1f);
}

#if false
Expand Down
9 changes: 6 additions & 3 deletions Assets/Scripts/Utils/AudioSourceHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,25 @@ public float Duration
}
}

public static void PlaySoundInterval(AudioSource audioSource, Section section, float pitch = 1.0f)
public static void PlaySoundInterval(AudioSource audioSource, Section section, float spatialBlend, float pitch = 1.0f)
{
audioSource.Stop();
audioSource.time = section.Start;
audioSource.pitch = pitch;
audioSource.spatialBlend = spatialBlend;
audioSource.Play();
audioSource.SetScheduledEndTime(AudioSettings.dspTime + section.Duration);
}

public static void PlayIntervalAtPoint(AudioClip audioClip, Section section, Vector3 position, float volume = 1.0f, float pitch = 1.0f)
public static void PlayIntervalAtPoint(AudioClip audioClip, Section section, Vector3 position, float spatialBlend, float volume = 1.0f, float pitch = 1.0f)
{
var gameObject = new GameObject(); // TODO(jason): There is no object pool here. Consider using the MMSoundManager from Feel framework.
var audioSource = gameObject.AddComponent<AudioSource>();
audioSource.clip = audioClip;
gameObject.transform.position = position;
audioSource.volume = volume;
audioSource.pitch = pitch;
audioSource.spatialBlend = spatialBlend;
audioSource.time = section.Start;
audioSource.Play();
audioSource.SetScheduledEndTime(AudioSettings.dspTime + section.Duration);
Expand All @@ -39,14 +41,15 @@ public static void PlayIntervalAtPoint(AudioClip audioClip, Section section, Vec
/// <summary>
/// A better version of AudioSource.PlayClipAtPoint. I don't know why, but the pitch on the built-in Unity method is weird.
/// </summary>
public static void PlayClipAtPoint(AudioClip audioClip, Vector3 position, float volume = 1.0f, float pitch = 1.0f)
public static void PlayClipAtPoint(AudioClip audioClip, Vector3 position, float spatialBlend, float volume = 1.0f, float pitch = 1.0f)
{
var gameObject = new GameObject(); // TODO(jason): There is no object pool here. Consider using the MMSoundManager from Feel framework.
var audioSource = gameObject.AddComponent<AudioSource>();
audioSource.clip = audioClip;
gameObject.transform.position = position;
audioSource.volume = volume;
audioSource.pitch = pitch;
audioSource.spatialBlend = spatialBlend;
audioSource.Play();
Object.Destroy(gameObject, audioClip.length);
}
Expand Down
1 change: 1 addition & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ Order | Feature | Pull Request
20 | Fix showstopper bugs for last release | https://github.com/nucleartide/Breadforge/pull/49
21 | Fix animation bugs for last release | https://github.com/nucleartide/Breadforge/pull/50
22 | Add ability to zoom in and out | https://github.com/nucleartide/Breadforge/pull/52
23 | Fix UI camera ortho size not updating; make sounds 3D via spatialBlend param | https://github.com/nucleartide/Breadforge/pull/53
... | *(more to come soon)* | *(more to come soon)*

---
Expand Down

0 comments on commit 7f1a122

Please sign in to comment.