2020-04-19 23:10:36 +02:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
[System.Serializable]
|
|
|
|
|
public enum StepType {
|
|
|
|
|
Left,
|
|
|
|
|
Right
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class CameraBobber : MonoBehaviour {
|
2020-04-20 01:20:02 +02:00
|
|
|
|
public Options Options;
|
2020-04-19 23:10:36 +02:00
|
|
|
|
public Transform CameraTransform;
|
|
|
|
|
public AnimationCurve HeadBobCurve = AnimationCurve.Constant(0, 1, 0);
|
|
|
|
|
public float StepDuration;
|
|
|
|
|
public float BobMagnitude;
|
|
|
|
|
public bool Moving = false;
|
|
|
|
|
public bool InAir = false;
|
|
|
|
|
public AudioSource FootstepSource;
|
|
|
|
|
// TODO: Vary by ground material
|
2020-04-20 04:05:26 +02:00
|
|
|
|
public AudioClip[] LandingFootstepClips;
|
2020-04-19 23:10:36 +02:00
|
|
|
|
public AudioClip[] RightFootstepClips;
|
|
|
|
|
public AudioClip[] LeftFootstepClips;
|
|
|
|
|
|
|
|
|
|
[Header("Runtime values")]
|
|
|
|
|
public StepType LastStep;
|
|
|
|
|
|
|
|
|
|
private float StepTime = 0;
|
|
|
|
|
private bool WasMoving = false;
|
|
|
|
|
private bool WasInAir = false;
|
|
|
|
|
private Vector3 BasePosition;
|
|
|
|
|
|
|
|
|
|
private void Start() {
|
|
|
|
|
BasePosition = CameraTransform.localPosition;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Update() {
|
|
|
|
|
if (Moving) {
|
|
|
|
|
StepTime += Time.deltaTime;
|
|
|
|
|
if (StepTime >= StepDuration) {
|
|
|
|
|
StepTime -= StepDuration;
|
|
|
|
|
if (!InAir) {
|
|
|
|
|
AlternateStep();
|
|
|
|
|
PlayStepSound(GetClips(LastStep), 1f);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
StepTime = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-20 01:20:02 +02:00
|
|
|
|
Vector3 TargetPosition = BasePosition;
|
|
|
|
|
if (Options.CameraBobbing) {
|
|
|
|
|
if (InAir) {
|
|
|
|
|
TargetPosition = BasePosition + Vector3.up * HeadBobCurve.Evaluate(0.5f) * BobMagnitude;
|
|
|
|
|
} else {
|
|
|
|
|
TargetPosition = BasePosition + Vector3.up * HeadBobCurve.Evaluate(StepTime / StepDuration) * BobMagnitude;
|
|
|
|
|
}
|
2020-04-19 23:10:36 +02:00
|
|
|
|
}
|
|
|
|
|
CameraTransform.localPosition = Vector3.Lerp(CameraTransform.localPosition, TargetPosition, 10f * Time.deltaTime);
|
|
|
|
|
|
2020-04-20 04:05:26 +02:00
|
|
|
|
if (Moving != WasMoving && !InAir) {
|
2020-04-19 23:10:36 +02:00
|
|
|
|
AlternateStep();
|
|
|
|
|
PlayStepSound(GetClips(LastStep), 0.7f);
|
2020-04-20 04:05:26 +02:00
|
|
|
|
} else if (WasInAir != InAir) {
|
|
|
|
|
AlternateStep();
|
|
|
|
|
PlayStepSound(LandingFootstepClips, 1f);
|
2020-04-19 23:10:36 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WasMoving = Moving;
|
|
|
|
|
WasInAir = InAir;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AlternateStep() {
|
|
|
|
|
LastStep = LastStep == StepType.Left ? StepType.Right : StepType.Left;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private AudioClip[] GetClips(StepType stepType) {
|
|
|
|
|
switch (stepType) {
|
|
|
|
|
default:
|
|
|
|
|
case StepType.Left:
|
|
|
|
|
return LeftFootstepClips;
|
|
|
|
|
case StepType.Right:
|
|
|
|
|
return RightFootstepClips;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void PlayStepSound(AudioClip[] clips, float volumeModifier) {
|
|
|
|
|
if (clips.Length > 0) {
|
|
|
|
|
FootstepSource.PlayOneShot(clips[Random.Range(0, clips.Length)], volumeModifier);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|