Add camera bobbing

This commit is contained in:
Jens Pitkänen 2020-04-20 00:10:36 +03:00
parent d3c0c642d2
commit 7abf582d1d
6 changed files with 328 additions and 8 deletions

View File

@ -30,6 +30,134 @@ Transform:
m_Father: {fileID: 6125707628839966534}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: -20, y: -20, z: 0}
--- !u!1 &5768207841704116667
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 6257962716474165078}
- component: {fileID: 827982312027890955}
m_Layer: 11
m_Name: Footstep Audio Source
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &6257962716474165078
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 5768207841704116667}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0.08, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 6125707630481988396}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!82 &827982312027890955
AudioSource:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 5768207841704116667}
m_Enabled: 1
serializedVersion: 4
OutputAudioMixerGroup: {fileID: 586271417499195193, guid: 032de345950918c4e89684078c6a7213,
type: 2}
m_audioClip: {fileID: 0}
m_PlayOnAwake: 0
m_Volume: 1
m_Pitch: 1
Loop: 0
Mute: 0
Spatialize: 0
SpatializePostEffects: 0
Priority: 128
DopplerLevel: 1
MinDistance: 1
MaxDistance: 500
Pan2D: 0
rolloffMode: 0
BypassEffects: 0
BypassListenerEffects: 0
BypassReverbZones: 0
rolloffCustomCurve:
serializedVersion: 2
m_Curve:
- serializedVersion: 3
time: 0
value: 1
inSlope: 0
outSlope: 0
tangentMode: 0
weightedMode: 0
inWeight: 0.33333334
outWeight: 0.33333334
- serializedVersion: 3
time: 1
value: 0
inSlope: 0
outSlope: 0
tangentMode: 0
weightedMode: 0
inWeight: 0.33333334
outWeight: 0.33333334
m_PreInfinity: 2
m_PostInfinity: 2
m_RotationOrder: 4
panLevelCustomCurve:
serializedVersion: 2
m_Curve:
- serializedVersion: 3
time: 0
value: 1
inSlope: 0
outSlope: 0
tangentMode: 0
weightedMode: 0
inWeight: 0.33333334
outWeight: 0.33333334
m_PreInfinity: 2
m_PostInfinity: 2
m_RotationOrder: 4
spreadCustomCurve:
serializedVersion: 2
m_Curve:
- serializedVersion: 3
time: 0
value: 0
inSlope: 0
outSlope: 0
tangentMode: 0
weightedMode: 0
inWeight: 0.33333334
outWeight: 0.33333334
m_PreInfinity: 2
m_PostInfinity: 2
m_RotationOrder: 4
reverbZoneMixCustomCurve:
serializedVersion: 2
m_Curve:
- serializedVersion: 3
time: 0
value: 1
inSlope: 0
outSlope: 0
tangentMode: 0
weightedMode: 0
inWeight: 0.33333334
outWeight: 0.33333334
m_PreInfinity: 2
m_PostInfinity: 2
m_RotationOrder: 4
--- !u!1 &6125707628839966531
GameObject:
m_ObjectHideFlags: 0
@ -188,6 +316,7 @@ GameObject:
- component: {fileID: 6125707630481988397}
- component: {fileID: 6125707630481988371}
- component: {fileID: 7093134810128755097}
- component: {fileID: 462937706053753531}
m_Layer: 11
m_Name: Player
m_TagString: Untagged
@ -207,6 +336,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children:
- {fileID: 6125707628839966534}
- {fileID: 6257962716474165078}
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: -16.035, z: 0}
@ -223,6 +353,7 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
GameState: {fileID: 0}
Options: {fileID: 0}
BodyTransform: {fileID: 6125707630481988396}
HeadTransform: {fileID: 6125707628839966534}
--- !u!114 &6125707630481988397
@ -241,7 +372,7 @@ MonoBehaviour:
JumpVelocity: 4
SlideVelocity: 3
JumpGracePeriod: 0.2
Antislipperiness: 20
Antislipperiness: 10
AntislipperinessInAir: 3
Grounded: 0
GroundNormal: {x: 0, y: 0, z: 0}
@ -282,6 +413,64 @@ MonoBehaviour:
ItemLayer:
serializedVersion: 2
m_Bits: 1024
DroppingRaycastMask:
serializedVersion: 2
m_Bits: 3895
Distance: 2
ThrowVelocity: 6
GrabbedItem: {fileID: 0}
--- !u!114 &462937706053753531
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6125707630481988399}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 46a5bf268fa1368419b104ac733e1835, type: 3}
m_Name:
m_EditorClassIdentifier:
CameraTransform: {fileID: 6125707628839966534}
HeadBobCurve:
serializedVersion: 2
m_Curve:
- serializedVersion: 3
time: 0
value: 0
inSlope: 0
outSlope: 0
tangentMode: 0
weightedMode: 0
inWeight: 0
outWeight: 0
- serializedVersion: 3
time: 0.5
value: 0.08
inSlope: -0.020761246
outSlope: -0.020761246
tangentMode: 0
weightedMode: 0
inWeight: 0.33333334
outWeight: 0.42499995
- serializedVersion: 3
time: 1
value: 0
inSlope: 0.008853439
outSlope: 0.008853439
tangentMode: 0
weightedMode: 0
inWeight: 0.49166667
outWeight: 0
m_PreInfinity: 2
m_PostInfinity: 1
m_RotationOrder: 4
StepDuration: 0.5
BobMagnitude: 1
MoveIntensity: 0
Moving: 0
InAir: 0
FootstepSource: {fileID: 827982312027890955}
RightFootstepClips: []
LeftFootstepClips: []
LastStep: 0

View File

@ -5478,6 +5478,11 @@ PrefabInstance:
m_Modification:
m_TransformParent: {fileID: 1921606960}
m_Modifications:
- target: {fileID: 226479021477264902, guid: 2cd64e49ad7ad1c4bb4a4264703d7915,
type: 3}
propertyPath: m_AnchoredPosition.y
value: 120
objectReference: {fileID: 0}
- target: {fileID: 317676106527232818, guid: 2cd64e49ad7ad1c4bb4a4264703d7915,
type: 3}
propertyPath: m_AnchorMax.y
@ -5488,11 +5493,26 @@ PrefabInstance:
propertyPath: m_AnchorMax.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 519962654072447509, guid: 2cd64e49ad7ad1c4bb4a4264703d7915,
type: 3}
propertyPath: m_AnchoredPosition.y
value: -50
objectReference: {fileID: 0}
- target: {fileID: 1470294652521727049, guid: 2cd64e49ad7ad1c4bb4a4264703d7915,
type: 3}
propertyPath: m_AnchorMax.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 3325963044810254397, guid: 2cd64e49ad7ad1c4bb4a4264703d7915,
type: 3}
propertyPath: m_AnchoredPosition.y
value: 60
objectReference: {fileID: 0}
- target: {fileID: 3479557468769067426, guid: 2cd64e49ad7ad1c4bb4a4264703d7915,
type: 3}
propertyPath: m_AnchoredPosition.y
value: 180
objectReference: {fileID: 0}
- target: {fileID: 3682198691915602031, guid: 2cd64e49ad7ad1c4bb4a4264703d7915,
type: 3}
propertyPath: m_AnchorMax.y
@ -5503,6 +5523,11 @@ PrefabInstance:
propertyPath: m_AnchorMax.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4127888187077662439, guid: 2cd64e49ad7ad1c4bb4a4264703d7915,
type: 3}
propertyPath: m_AnchoredPosition.y
value: -100
objectReference: {fileID: 0}
- target: {fileID: 5400695020334462788, guid: 2cd64e49ad7ad1c4bb4a4264703d7915,
type: 3}
propertyPath: m_Alpha
@ -5553,6 +5578,11 @@ PrefabInstance:
propertyPath: m_Name
value: Pause Menu
objectReference: {fileID: 0}
- target: {fileID: 6081099778283769688, guid: 2cd64e49ad7ad1c4bb4a4264703d7915,
type: 3}
propertyPath: m_AnchoredPosition.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 6723009973072559076, guid: 2cd64e49ad7ad1c4bb4a4264703d7915,
type: 3}
propertyPath: m_AnchorMax.y
@ -5563,6 +5593,11 @@ PrefabInstance:
propertyPath: m_AnchorMax.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 7379875903275102228, guid: 2cd64e49ad7ad1c4bb4a4264703d7915,
type: 3}
propertyPath: m_AnchoredPosition.y
value: 240
objectReference: {fileID: 0}
- target: {fileID: 8362288861398798554, guid: 2cd64e49ad7ad1c4bb4a4264703d7915,
type: 3}
propertyPath: m_AnchorMax.y
@ -5684,11 +5719,6 @@ PrefabInstance:
propertyPath: GrabText
value:
objectReference: {fileID: 1849724586}
- target: {fileID: 7093134810128755097, guid: 558201eae20fa5540a826edb23937665,
type: 3}
propertyPath: DroppingRaycastMask.m_Bits
value: 3895
objectReference: {fileID: 0}
m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 558201eae20fa5540a826edb23937665, type: 3}
--- !u!1001 &6166170816655763453

View File

@ -0,0 +1,85 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public enum StepType {
Left,
Right
}
public class CameraBobber : MonoBehaviour {
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
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;
}
Vector3 TargetPosition;
if (InAir) {
TargetPosition = BasePosition + Vector3.up * HeadBobCurve.Evaluate(0.5f) * BobMagnitude;
} else {
TargetPosition = BasePosition + Vector3.up * HeadBobCurve.Evaluate(StepTime / StepDuration) * BobMagnitude;
}
CameraTransform.localPosition = Vector3.Lerp(CameraTransform.localPosition, TargetPosition, 10f * Time.deltaTime);
if ((Moving != WasMoving && !InAir) || WasInAir != InAir) {
AlternateStep();
PlayStepSound(GetClips(LastStep), 0.7f);
}
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);
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 46a5bf268fa1368419b104ac733e1835
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -7,7 +7,6 @@ public class PauseMenu : MonoBehaviour {
public Options Options;
[Header("Option inputs")]
public Toggle InvertMouseX;
public Toggle InvertMouseY;
public Toggle CameraBobbing;
public Slider MouseSensitivity;
@ -29,7 +28,6 @@ public class PauseMenu : MonoBehaviour {
}
private void Start() {
InvertMouseX.isOn = Options.InvertMouseX;
InvertMouseY.isOn = Options.InvertMouseY;
CameraBobbing.isOn = Options.CameraBobbing;
MouseSensitivity.value = Options.MouseSensitivity;

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(CharacterController))]
[RequireComponent(typeof(CameraBobber))]
public class PlayerController : MonoBehaviour {
public float MovementSpeed;
public float JumpVelocity;
@ -19,6 +20,7 @@ public class PlayerController : MonoBehaviour {
public Vector3 GroundNormal;
private CharacterController Character;
private CameraBobber Bobber;
private Vector3 GroundVelocity = new Vector3();
private float FallingVelocity;
@ -27,6 +29,7 @@ public class PlayerController : MonoBehaviour {
private void Awake() {
Character = GetComponent<CharacterController>();
Bobber = GetComponent<CameraBobber>();
}
private void Start() {
@ -89,6 +92,10 @@ public class PlayerController : MonoBehaviour {
CurrentVelocity += Vector3.ProjectOnPlane(-Vector3.up, GroundNormal) * SlideVelocity;
}
Bobber.BobMagnitude = Mathf.Lerp(Bobber.BobMagnitude, TargetSpeed > 0 ? GroundVelocity.magnitude / TargetSpeed : 1, 5f * Time.deltaTime);
Bobber.Moving = GroundVelocity.magnitude > 0.1f;
Bobber.InAir = !Grounded;
Character.Move(CurrentVelocity * Time.deltaTime);
}