From 45eec4e180d61b633df9ae7c76dfd39255ab253d Mon Sep 17 00:00:00 2001 From: Ari Vuollet Date: Sun, 24 Apr 2022 12:45:56 +0300 Subject: [PATCH] smoothen camera in stairs --- Content/Common/PlayerPrefab.prefab | 198 +++++++++++++++-------------- Content/Scenes/AerowalkScene.scene | 59 +++++---- Source/Game/CameraSpring.cs | 71 +++++++++++ 3 files changed, 208 insertions(+), 120 deletions(-) create mode 100644 Source/Game/CameraSpring.cs diff --git a/Content/Common/PlayerPrefab.prefab b/Content/Common/PlayerPrefab.prefab index 20f6671..d9d0578 100644 --- a/Content/Common/PlayerPrefab.prefab +++ b/Content/Common/PlayerPrefab.prefab @@ -50,6 +50,12 @@ } } }, + { + "ID": "25fbe7024ef684d3b5141eaf9713c973", + "TypeName": "Game.CameraSpring", + "ParentID": "51c770f24232abbb112cc98b296820d8", + "V": {} + }, { "ID": "eb46ab96465957dc67a052bd0e2ad1e5", "TypeName": "FlaxEngine.Camera", @@ -88,6 +94,102 @@ "Name": "AudioListener", "StaticFlags": 0 }, + { + "ID": "56eddcef4698702bd3cb0b8a1fb3396f", + "TypeName": "FlaxEngine.EmptyActor", + "ParentID": "a50f3639419a8306036ecfab7115e772", + "Name": "ViewModelHolder", + "Transform": { + "Translation": { + "X": 0.0, + "Y": 22.0, + "Z": 0.0 + } + }, + "Layer": 2 + }, + { + "ID": "195f796349961ef9a9d46a8657fc693b", + "TypeName": "Game.WeaponSway", + "ParentID": "56eddcef4698702bd3cb0b8a1fb3396f", + "V": { + "swaySpeed": 1990.0 +} + }, + { + "ID": "111ba6ba4129558d16c0629bb31a55f9", + "TypeName": "FlaxEngine.StaticModel", + "ParentID": "56eddcef4698702bd3cb0b8a1fb3396f", + "Name": "DummyWeapon", + "Transform": { + "Translation": { + "X": 7.394217491149902, + "Y": -11.22248649597168, + "Z": -3.27701997756958 + }, + "Orientation": { + "X": 0.6691311001777649, + "Y": 0.0, + "Z": 0.0, + "W": 0.7431443929672241 + }, + "Scale": { + "X": 0.07999999821186066, + "Y": 0.431741327047348, + "Z": 0.07999999821186066 + } + }, + "StaticFlags": 0, + "Layer": 2, + "Model": "223aa9894e90973780ab508621517526", + "Buffer": { + "Entries": [ + { + "Material": "ae5c8a4b4f723b60b8b31290a9dfb21a", + "ShadowsMode": 3, + "Visible": true, + "ReceiveDecals": true + } + ] + } + }, + { + "ID": "99a5bfe4459e3ac2c3f0198ede38a587", + "TypeName": "FlaxEngine.StaticModel", + "ParentID": "56eddcef4698702bd3cb0b8a1fb3396f", + "Name": "DummyWeapon 0", + "Transform": { + "Translation": { + "X": 7.35720682144165, + "Y": -9.197076797485352, + "Z": 15.981095314025879 + }, + "Orientation": { + "X": 0.6691311001777649, + "Y": 0.0, + "Z": 0.0, + "W": 0.7431443929672241 + }, + "Scale": { + "X": 0.09000000357627869, + "Y": 0.05855093151330948, + "Z": 0.09000000357627869 + } + }, + "StaticFlags": 0, + "Layer": 2, + "Model": "223aa9894e90973780ab508621517526", + "Buffer": { + "Entries": [ + { + "Material": "ae5c8a4b4f723b60b8b31290a9dfb21a", + "ShadowsMode": 3, + "Visible": true, + "ReceiveDecals": true + } + ] + } + }, { "ID": "a5850c1640208268c45f2892fe6b9e45", "TypeName": "FlaxEngine.CapsuleCollider", @@ -143,102 +245,6 @@ }, "StaticFlags": 0, "CollisionData": "593d92914c4bd54679ddec9e539bba80" - }, - { - "ID": "56eddcef4698702bd3cb0b8a1fb3396f", - "TypeName": "FlaxEngine.EmptyActor", - "ParentID": "a50f3639419a8306036ecfab7115e772", - "Name": "ViewModelHolder", - "Transform": { - "Translation": { - "X": 0.0, - "Y": 22.0, - "Z": 0.0 - } - }, - "Layer": 2 - }, - { - "ID": "195f796349961ef9a9d46a8657fc693b", - "TypeName": "Game.WeaponSway", - "ParentID": "56eddcef4698702bd3cb0b8a1fb3396f", - "V": { - "swaySpeed": 90.0 -} - }, - { - "ID": "111ba6ba4129558d16c0629bb31a55f9", - "TypeName": "FlaxEngine.StaticModel", - "ParentID": "56eddcef4698702bd3cb0b8a1fb3396f", - "Name": "DummyWeapon", - "Transform": { - "Translation": { - "X": 7.394217491149902, - "Y": -11.22248649597168, - "Z": -3.27701997756958 - }, - "Orientation": { - "X": 0.6691311001777649, - "Y": 0.0, - "Z": 0.0, - "W": 0.7431443929672241 - }, - "Scale": { - "X": 0.07999999821186066, - "Y": 0.431741327047348, - "Z": 0.07999999821186066 - } - }, - "StaticFlags": 0, - "Layer": 2, - "Model": "223aa9894e90973780ab508621517526", - "Buffer": { - "Entries": [ - { - "Material": "59d8bf5b4addd14c1e8f6089d1c3577c", - "ShadowsMode": 3, - "Visible": true, - "ReceiveDecals": true - } - ] - } - }, - { - "ID": "99a5bfe4459e3ac2c3f0198ede38a587", - "TypeName": "FlaxEngine.StaticModel", - "ParentID": "56eddcef4698702bd3cb0b8a1fb3396f", - "Name": "DummyWeapon 0", - "Transform": { - "Translation": { - "X": 7.35720682144165, - "Y": -9.197076797485352, - "Z": 15.981095314025879 - }, - "Orientation": { - "X": 0.6691311001777649, - "Y": 0.0, - "Z": 0.0, - "W": 0.7431443929672241 - }, - "Scale": { - "X": 0.09000000357627869, - "Y": 0.05855093151330948, - "Z": 0.09000000357627869 - } - }, - "StaticFlags": 0, - "Layer": 2, - "Model": "223aa9894e90973780ab508621517526", - "Buffer": { - "Entries": [ - { - "Material": "ae5c8a4b4f723b60b8b31290a9dfb21a", - "ShadowsMode": 3, - "Visible": true, - "ReceiveDecals": true - } - ] - } } ] } \ No newline at end of file diff --git a/Content/Scenes/AerowalkScene.scene b/Content/Scenes/AerowalkScene.scene index 2accec9..31d6295 100644 --- a/Content/Scenes/AerowalkScene.scene +++ b/Content/Scenes/AerowalkScene.scene @@ -70,13 +70,13 @@ "Transform": { "Translation": { "X": 0.0, - "Y": 716.0, + "Y": 751.0, "Z": 0.0 } }, "Control": "FlaxEngine.GUI.Label", "Data": { - "Text": "eFPS: 119 uTime: 41.0757171\nuFPS: 120 uTime: 0\nrFPS: 120 rTime: 0\npFPS: 30 pTime: 0", + "Text": "eFPS: 120 uTime: 3.3759838\nuFPS: 120 uTime: 0\nrFPS: 120 rTime: 0\npFPS: 30 pTime: 0", "TextColor": { "R": 1.0, "G": 1.0, @@ -123,7 +123,7 @@ }, "Offsets": { "Left": 0.0, - "Right": 168.0, + "Right": 161.0, "Top": -97.0, "Bottom": 64.0 }, @@ -234,6 +234,19 @@ "PrefabObjectID": "51c770f24232abbb112cc98b296820d8", "ParentID": "229033d34bad347b1982ac9a6dcf66f4" }, + { + "ID": "5a8b2a254a497d86d38aceac8daea934", + "PrefabID": "82e58c9d462fba5a0df1a599417ff684", + "PrefabObjectID": "25fbe7024ef684d3b5141eaf9713c973", + "ParentID": "f4ab880f47cab1ca423ce8b04f524ac2", + "V": {} + }, + { + "ID": "5a96096947dadddc8576978ffc6ee455", + "TypeName": "Game.CameraSpring", + "ParentID": "f4ab880f47cab1ca423ce8b04f524ac2", + "V": {} + }, { "ID": "86dbba284fbe908c9e62b89493e9a83f", "PrefabID": "82e58c9d462fba5a0df1a599417ff684", @@ -261,24 +274,6 @@ "PrefabObjectID": "262c688d49caaf66eb7e6a97f0d01206", "ParentID": "f4ab880f47cab1ca423ce8b04f524ac2" }, - { - "ID": "b8dde5a34570002b6b5666a89d6a70cb", - "PrefabID": "82e58c9d462fba5a0df1a599417ff684", - "PrefabObjectID": "a5850c1640208268c45f2892fe6b9e45", - "ParentID": "59cc65774b36b4a58a274fb7b4e9d490" - }, - { - "ID": "3673873e4300efe664dfc7b743fa1b80", - "PrefabID": "82e58c9d462fba5a0df1a599417ff684", - "PrefabObjectID": "0dcd9f8d42d9e574e45af8b8bdad373d", - "ParentID": "59cc65774b36b4a58a274fb7b4e9d490" - }, - { - "ID": "1c9fb3124b9c1ae1e704f59267d3e687", - "PrefabID": "82e58c9d462fba5a0df1a599417ff684", - "PrefabObjectID": "3932111d4c6f925a7b4a2e912307c82a", - "ParentID": "59cc65774b36b4a58a274fb7b4e9d490" - }, { "ID": "f3b9d1594b1f2fc5b793adb5f144ab46", "PrefabID": "82e58c9d462fba5a0df1a599417ff684", @@ -290,9 +285,7 @@ "PrefabID": "82e58c9d462fba5a0df1a599417ff684", "PrefabObjectID": "195f796349961ef9a9d46a8657fc693b", "ParentID": "f3b9d1594b1f2fc5b793adb5f144ab46", - "V": { - "swaySpeed": 1990.0 -} + "V": {} }, { "ID": "d267a9954013a72391bd3a921d214639", @@ -315,6 +308,24 @@ {} ] } + }, + { + "ID": "b8dde5a34570002b6b5666a89d6a70cb", + "PrefabID": "82e58c9d462fba5a0df1a599417ff684", + "PrefabObjectID": "a5850c1640208268c45f2892fe6b9e45", + "ParentID": "59cc65774b36b4a58a274fb7b4e9d490" + }, + { + "ID": "3673873e4300efe664dfc7b743fa1b80", + "PrefabID": "82e58c9d462fba5a0df1a599417ff684", + "PrefabObjectID": "0dcd9f8d42d9e574e45af8b8bdad373d", + "ParentID": "59cc65774b36b4a58a274fb7b4e9d490" + }, + { + "ID": "1c9fb3124b9c1ae1e704f59267d3e687", + "PrefabID": "82e58c9d462fba5a0df1a599417ff684", + "PrefabObjectID": "3932111d4c6f925a7b4a2e912307c82a", + "ParentID": "59cc65774b36b4a58a274fb7b4e9d490" } ] } \ No newline at end of file diff --git a/Source/Game/CameraSpring.cs b/Source/Game/CameraSpring.cs new file mode 100644 index 0000000..e7ca0a2 --- /dev/null +++ b/Source/Game/CameraSpring.cs @@ -0,0 +1,71 @@ +using System; +using System.Runtime; +using FlaxEditor.Content.Settings; +using FlaxEngine; +using Console = Cabrito.Console; + + +namespace Game +{ + public class CameraSpring : Script + { + private Vector3 targetOffset; + private Vector3 lastPosition; + + public float speed = 240f; + public float percY; + + private Actor playerActor; + private Actor viewModelHolder; + private PlayerMovement playerMovement; + + private bool lastGround; + + public override void OnStart() + { + playerActor = Actor.Parent.Parent; + playerMovement = playerActor.GetScript(); + viewModelHolder = playerActor.GetChild("ViewModelHolder"); + + lastGround = playerMovement.onGround; + targetOffset = Actor.LocalPosition; + } + + private void UpdatePosition(Vector3 position) + { + Actor.Position = position; + viewModelHolder.Position = position; + } + + public override void OnUpdate() + { + Vector3 position = Actor.Parent.Position + targetOffset; + Vector3 targetPosition = position; + + if (playerMovement.onGround) + { + float deltaY = position.Y - lastPosition.Y; + //if (Mathf.Abs(deltaY) < 10f) + if (deltaY > 0) + { + const float catchUpDistance = 10f; + const float catchUpMinMultip = 0.25f; + percY = Mathf.Abs(deltaY) / catchUpDistance; + percY = Mathf.Min(1.0f, percY + catchUpMinMultip); + percY *= percY; + + float adjustSpeed = speed * Time.DeltaTime * percY; + + position.Y = lastPosition.Y; //-= deltaY; + position.Y = Mathf.MoveTowards(position.Y, targetPosition.Y, adjustSpeed); + UpdatePosition(position); + } + } + else + UpdatePosition(position); + + lastPosition = position; + lastGround = playerMovement.onGround; + } + } +} \ No newline at end of file