+玩家行为事件
+视角摇晃 +相机后处理
This commit is contained in:
35
Assets/Scripts/Camera/CameraPostProcessing.cs
Normal file
35
Assets/Scripts/Camera/CameraPostProcessing.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using Event;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Rendering.PostProcessing;
|
||||
|
||||
namespace Camera
|
||||
{
|
||||
public class CameraPostProcessing : MonoBehaviour
|
||||
{
|
||||
private UnityEngine.Camera _camera;
|
||||
private PostProcessVolume _processVolume;
|
||||
private void Awake()
|
||||
{
|
||||
_camera = GetComponent<UnityEngine.Camera>();
|
||||
_processVolume = GetComponent<PostProcessVolume>();
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
EventManager.Instance.PlayerRunning += PostProcess;
|
||||
EventManager.Instance.PlayerRunStop += ProcessingStop;
|
||||
}
|
||||
|
||||
private void PostProcess()
|
||||
{
|
||||
_camera.fieldOfView = Mathf.Lerp(_camera.fieldOfView, 90f, Time.deltaTime * 10f);
|
||||
_processVolume.profile.GetSetting<ChromaticAberration>().intensity.value = Mathf.Lerp(_processVolume.profile.GetSetting<ChromaticAberration>().intensity.value, 1f, Time.deltaTime * 10f);
|
||||
}
|
||||
|
||||
private void ProcessingStop()
|
||||
{
|
||||
_processVolume.profile.GetSetting<ChromaticAberration>().intensity.value = Mathf.Lerp(_processVolume.profile.GetSetting<ChromaticAberration>().intensity.value, 0f, Time.deltaTime * 10f);
|
||||
_camera.fieldOfView = Mathf.Lerp(_camera.fieldOfView, 60f, Time.deltaTime * 10f);
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Camera/CameraPostProcessing.cs.meta
Normal file
3
Assets/Scripts/Camera/CameraPostProcessing.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: dd8e51b43a0c4d4ca7e732246b8ad2e6
|
||||
timeCreated: 1726297370
|
||||
68
Assets/Scripts/Camera/CameraShake.cs
Normal file
68
Assets/Scripts/Camera/CameraShake.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
using Event;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Camera
|
||||
{
|
||||
public class CameraShake : MonoBehaviour
|
||||
{
|
||||
public Transform cameraTransform;
|
||||
public float amplitude = 0.05f;
|
||||
public float frequency = 10.0f;
|
||||
|
||||
private Vector3 _originalPos;
|
||||
private bool _isRunning;
|
||||
private bool _isWalking;
|
||||
private void Start()
|
||||
{
|
||||
_originalPos = cameraTransform.localPosition;
|
||||
EventManager.Instance.PlayerRunning += OnPlayerRunning;
|
||||
EventManager.Instance.PlayerRunStop += StopRunning;
|
||||
EventManager.Instance.PlayerWalking += OnPlayerWalking;
|
||||
EventManager.Instance.PlayerWalkStop += StopWalking;
|
||||
}
|
||||
|
||||
|
||||
private void OnPlayerRunning()
|
||||
{
|
||||
_isRunning = true;
|
||||
}
|
||||
|
||||
private void OnPlayerWalking()
|
||||
{
|
||||
_isWalking = true;
|
||||
}
|
||||
|
||||
private void StopRunning()
|
||||
{
|
||||
_isRunning = false;
|
||||
}
|
||||
|
||||
private void StopWalking()
|
||||
{
|
||||
_isWalking = false;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (_isRunning)
|
||||
{
|
||||
var xShake = Mathf.Sin(Time.time * frequency) * amplitude;
|
||||
var yShake = Mathf.Cos(Time.time * frequency * 2) * amplitude * 0.5f;
|
||||
|
||||
cameraTransform.localPosition = _originalPos + new Vector3(xShake, yShake, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_isWalking)
|
||||
{
|
||||
var xShake = Mathf.Sin(Time.time * frequency / 2) * amplitude;
|
||||
var yShake = Mathf.Cos(Time.time * frequency / 2 * 2) * amplitude * 0.5f;
|
||||
|
||||
cameraTransform.localPosition = _originalPos + new Vector3(xShake, yShake, 0);
|
||||
}else
|
||||
cameraTransform.localPosition = _originalPos;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Camera/CameraShake.cs.meta
Normal file
3
Assets/Scripts/Camera/CameraShake.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d64f284209414d79a3996a0a6d1748b4
|
||||
timeCreated: 1726297975
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace Event.EventHandler
|
||||
{
|
||||
public delegate void PlayerRunStartHandler();
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 80c4ab8259fd439ab961f53ca470750b
|
||||
timeCreated: 1726298205
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace Event.EventHandler
|
||||
{
|
||||
public delegate void PlayerRunStopHandler();
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e59b8b89219149f3b17c92a1fcb47166
|
||||
timeCreated: 1726297713
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace Event.EventHandler
|
||||
{
|
||||
public delegate void PlayerRunningHandler();
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7a6ecbb68bac4f7d9631c32554b2c191
|
||||
timeCreated: 1726297312
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace Event.EventHandler
|
||||
{
|
||||
public delegate void PlayerWalkStartHandler();
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0a1e2607d7a84dab8be4a3433c5a88c1
|
||||
timeCreated: 1726298933
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace Event.EventHandler
|
||||
{
|
||||
public delegate void PlayerWalkStopHandler();
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2cd3785317b44e7fae2becaae10a3029
|
||||
timeCreated: 1726298918
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace Event.EventHandler
|
||||
{
|
||||
public delegate void PlayerWalkingHandler();
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 38833c7bee844164a50a86b10a7790ee
|
||||
timeCreated: 1726298881
|
||||
@@ -34,6 +34,14 @@ namespace Event
|
||||
|
||||
public event CameraInterActHandler CameraInterAct;
|
||||
public event DialogPopHandler DialogPop;
|
||||
|
||||
public event PlayerRunStartHandler PlayerRunStart;
|
||||
public event PlayerRunningHandler PlayerRunning;
|
||||
|
||||
public event PlayerRunStopHandler PlayerRunStop;
|
||||
public event PlayerWalkStartHandler PlayerWalkStart;
|
||||
public event PlayerWalkStopHandler PlayerWalkStop;
|
||||
public event PlayerWalkingHandler PlayerWalking;
|
||||
|
||||
public void OnCameraInterAct(GameObject item)
|
||||
{
|
||||
@@ -44,5 +52,34 @@ namespace Event
|
||||
{
|
||||
DialogPop?.Invoke(new DialogPopArgs(index));
|
||||
}
|
||||
|
||||
public void OnPlayerWalkStart()
|
||||
{
|
||||
PlayerWalkStart?.Invoke();
|
||||
}
|
||||
|
||||
public void OnPlayerWalkStop()
|
||||
{
|
||||
PlayerWalkStop?.Invoke();
|
||||
}
|
||||
|
||||
public void OnPlayerWalking()
|
||||
{
|
||||
PlayerWalking?.Invoke();
|
||||
}
|
||||
|
||||
public void OnPlayerRunStart()
|
||||
{
|
||||
PlayerRunStart?.Invoke();
|
||||
}
|
||||
public void OnPlayerRunning()
|
||||
{
|
||||
PlayerRunning?.Invoke();
|
||||
}
|
||||
|
||||
public void OnPlayerRunStop()
|
||||
{
|
||||
PlayerRunStop?.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -58,6 +58,7 @@ namespace Keyboard
|
||||
keyMappings.Add(new KeyMapping("Right", KeyCode.D));
|
||||
keyMappings.Add(new KeyMapping("Up", KeyCode.W));
|
||||
keyMappings.Add(new KeyMapping("Down", KeyCode.S));
|
||||
keyMappings.Add(new KeyMapping("Run", KeyCode.LeftShift));
|
||||
SaveKeySettings();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
using Keyboard;
|
||||
using Event;
|
||||
using Keyboard;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Rendering.PostProcessing;
|
||||
|
||||
namespace Player
|
||||
{
|
||||
@@ -8,7 +10,11 @@ namespace Player
|
||||
[SerializeField] private Rigidbody rb;
|
||||
[SerializeField] private Animator animator;
|
||||
[SerializeField] private float maxVelocity;
|
||||
[SerializeField] private PostProcessVolume postProcessVolume;
|
||||
[SerializeField] private UnityEngine.Camera postProcessCamera;
|
||||
private Vector3 _velocity;
|
||||
private bool _isRunning;
|
||||
private bool _isWalking;
|
||||
|
||||
private void Update()
|
||||
{
|
||||
@@ -26,7 +32,36 @@ namespace Player
|
||||
temp.y = rb.velocity.y;
|
||||
rb.velocity = temp;
|
||||
}
|
||||
rb.velocity = Vector3.ClampMagnitude(rb.velocity, maxVelocity);
|
||||
if (Input.GetKey(KeySettingManager.Instance.GetKey("Run")) && KeySettingManager.Instance.Direction != Vector2.zero)
|
||||
{
|
||||
if (!_isRunning)
|
||||
{
|
||||
_isRunning = true;
|
||||
_isWalking = false;
|
||||
EventManager.Instance.OnPlayerWalkStop();
|
||||
EventManager.Instance.OnPlayerRunStart();
|
||||
}
|
||||
EventManager.Instance.OnPlayerRunning();
|
||||
rb.velocity = Vector3.ClampMagnitude(rb.velocity, maxVelocity+2);
|
||||
}
|
||||
if (!Input.GetKey(KeySettingManager.Instance.GetKey("Run")) && KeySettingManager.Instance.Direction != Vector2.zero)
|
||||
{
|
||||
if (!_isWalking)
|
||||
{
|
||||
_isRunning = false;
|
||||
_isWalking = true;
|
||||
EventManager.Instance.OnPlayerRunStop();
|
||||
EventManager.Instance.OnPlayerWalkStart();
|
||||
}
|
||||
EventManager.Instance.OnPlayerWalking();
|
||||
rb.velocity = Vector3.ClampMagnitude(rb.velocity, maxVelocity);
|
||||
}
|
||||
|
||||
if (KeySettingManager.Instance.Direction == Vector2.zero)
|
||||
{
|
||||
EventManager.Instance.OnPlayerRunStop();
|
||||
EventManager.Instance.OnPlayerWalkStop();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user