145 lines
4.1 KiB
C#
145 lines
4.1 KiB
C#
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using UnityEngine;
|
||
|
using UnityEngine.XR;
|
||
|
using UnityEngine.Rendering;
|
||
|
using UnityEngine.Rendering.Universal;
|
||
|
using UnityEngine.XR.Interaction.Toolkit;
|
||
|
public class Dash : MonoBehaviour
|
||
|
{
|
||
|
[SerializeField]
|
||
|
private float minDashRange = 0.5f;
|
||
|
[SerializeField]
|
||
|
private float maxDashRange = 40f;
|
||
|
[SerializeField]
|
||
|
private float dashTime = 0.2f;
|
||
|
|
||
|
private Transform cameraRigRoot;
|
||
|
public XRNode dashSource;
|
||
|
public InputHelpers.Button dashButton;
|
||
|
private float inputThreshold = 0.1f;
|
||
|
|
||
|
public float vignetteIntensity = 0.75f;
|
||
|
public float abberationIntensity = 0.75f;
|
||
|
public float duration = 0.5f;
|
||
|
public Volume volume = null;
|
||
|
private Transform rController;
|
||
|
|
||
|
private ParticleSystem trail;
|
||
|
private Vignette vignette = null;
|
||
|
private ChromaticAberration aberration = null;
|
||
|
|
||
|
public AudioClip dashFX;
|
||
|
private bool _canDash = true;
|
||
|
private bool _justDashed = false;
|
||
|
private float _lastUsed;
|
||
|
private float _dashCooldown = 3.0f;
|
||
|
AudioSource audioPlayer;
|
||
|
|
||
|
private void Start()
|
||
|
{
|
||
|
trail = transform.Find("Trail").GetComponent<ParticleSystem>();
|
||
|
rController = GameObject.Find("RightHand Controller").transform;
|
||
|
cameraRigRoot = transform;
|
||
|
if (volume.profile.TryGet(out Vignette vignette))
|
||
|
{
|
||
|
this.vignette = vignette;
|
||
|
vignette.smoothness.Override(0.4f);
|
||
|
}
|
||
|
if (volume.profile.TryGet(out ChromaticAberration aberration))
|
||
|
{
|
||
|
this.aberration = aberration;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private void Update()
|
||
|
{
|
||
|
|
||
|
InputHelpers.IsPressed(InputDevices.GetDeviceAtXRNode(dashSource), dashButton, out bool isPressed, inputThreshold);
|
||
|
if (isPressed && _canDash)
|
||
|
{
|
||
|
TryDash();
|
||
|
}
|
||
|
|
||
|
if (_justDashed)
|
||
|
{
|
||
|
if (_lastUsed + _dashCooldown < Time.time)
|
||
|
{
|
||
|
_justDashed = false;
|
||
|
_canDash = true;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
private void TryDash()
|
||
|
{
|
||
|
RaycastHit hit;
|
||
|
Ray ray = new Ray(rController.position, rController.forward);
|
||
|
if (Physics.Raycast(ray, out hit))
|
||
|
{
|
||
|
Debug.Log(hit);
|
||
|
if (hit.distance > minDashRange && hit.distance < maxDashRange)
|
||
|
{
|
||
|
_canDash = false;
|
||
|
_justDashed = true;
|
||
|
_lastUsed = Time.time;
|
||
|
StartCoroutine(FadeAndAbbr(0, vignetteIntensity, 0, abberationIntensity));
|
||
|
StartCoroutine(DoDash(hit.point));
|
||
|
StartCoroutine(ExecuteAfterTime(1));
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
private IEnumerator DoDash(Vector3 endPoint)
|
||
|
{
|
||
|
|
||
|
float elapsed = 0f;
|
||
|
AudioSource.PlayClipAtPoint(dashFX, transform.position, 1);
|
||
|
Vector3 startPoint = cameraRigRoot.position;
|
||
|
trail.Play();
|
||
|
while (elapsed < dashTime)
|
||
|
{
|
||
|
elapsed += Time.deltaTime;
|
||
|
float elapsedPct = elapsed / dashTime;
|
||
|
cameraRigRoot.position = Vector3.Lerp(startPoint, endPoint, elapsedPct);
|
||
|
yield return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
IEnumerator ExecuteAfterTime(float time)
|
||
|
{
|
||
|
yield return new WaitForSeconds(time);
|
||
|
|
||
|
// Code to execute after the delay
|
||
|
StartCoroutine(FadeAndAbbr(vignetteIntensity, 0, abberationIntensity, 0));
|
||
|
}
|
||
|
|
||
|
private IEnumerator FadeAndAbbr(float VstartValue, float VendValue, float CAstartValue, float CAendValue)
|
||
|
{
|
||
|
float elapsedTime = 0.0f;
|
||
|
|
||
|
while (elapsedTime <= duration)
|
||
|
{
|
||
|
float blend = elapsedTime / duration;
|
||
|
elapsedTime += Time.deltaTime;
|
||
|
|
||
|
float Vintensity = Mathf.Lerp(VstartValue, VendValue, blend);
|
||
|
float CAintensity = Mathf.Lerp(CAstartValue, CAendValue, blend);
|
||
|
ApplyValue(Vintensity, CAintensity);
|
||
|
}
|
||
|
|
||
|
yield return null;
|
||
|
|
||
|
}
|
||
|
|
||
|
private void ApplyValue(float vignette, float cromabbr)
|
||
|
{
|
||
|
this.vignette.intensity.Override(vignette);
|
||
|
aberration.intensity.Override(cromabbr);
|
||
|
}
|
||
|
}
|