Skip to content

Commit

Permalink
Player shakiness fix, Unity update
Browse files Browse the repository at this point in the history
Fixed shakiness described in issues SebLague#13 and SebLague#18 , Unity updated to 2020.3.1f1 (77a89f25062f)
  • Loading branch information
Delofon committed Mar 31, 2021
1 parent 389eb16 commit a265caa
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 43 deletions.
62 changes: 37 additions & 25 deletions Assets/Scripts/Game/Controllers/PlayerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public class PlayerController : GravityObject {
float pitch;
float smoothYaw;
float smoothPitch;

float smoothYawOld;

float yawSmoothV;
float pitchSmoothV;
Expand All @@ -45,6 +47,8 @@ public class PlayerController : GravityObject {
Vector3 cameraLocalPos;
Vector3 smoothVelocity;
Vector3 smoothVRef;

bool isGrounded;

// Jetpack
bool usingJetpack;
Expand Down Expand Up @@ -77,33 +81,50 @@ void InitRigidbody () {
}

void Update () {
HandleMovement ();
}
HandleInput();

void HandleMovement () {
HandleEditorInput ();
if (Time.timeScale == 0) {
return;
// Refuel jetpack
if (Time.time - lastJetpackUseTime > jetpackRefuelDelay) {
jetpackFuelPercent = Mathf.Clamp01 (jetpackFuelPercent + Time.deltaTime / jetpackRefuelTime);
}

// Handle animations
float currentSpeed = smoothVelocity.magnitude;
float animationSpeedPercent = (currentSpeed <= walkSpeed) ? currentSpeed / walkSpeed / 2 : currentSpeed / runSpeed;
animator.SetBool ("Grounded", isGrounded);
animator.SetFloat ("Speed", animationSpeedPercent);
}

void HandleInput()
{
HandleEditorInput();

// Look input
yaw += Input.GetAxisRaw ("Mouse X") * inputSettings.mouseSensitivity / 10 * mouseSensitivityMultiplier;
pitch -= Input.GetAxisRaw ("Mouse Y") * inputSettings.mouseSensitivity / 10 * mouseSensitivityMultiplier;
pitch = Mathf.Clamp (pitch, pitchMinMax.x, pitchMinMax.y);
float mouseSmoothTime = Mathf.Lerp (0.01f, maxMouseSmoothTime, inputSettings.mouseSmoothing);
smoothPitch = Mathf.SmoothDampAngle (smoothPitch, pitch, ref pitchSmoothV, mouseSmoothTime);
float smoothYawOld = smoothYaw;
smoothYawOld = smoothYaw;
smoothYaw = Mathf.SmoothDampAngle (smoothYaw, yaw, ref yawSmoothV, mouseSmoothTime);
if (!debug_playerFrozen && Time.timeScale > 0) {
cam.transform.localEulerAngles = Vector3.right * smoothPitch;
transform.Rotate (Vector3.up * Mathf.DeltaAngle (smoothYawOld, smoothYaw), Space.Self);
}

// Movement
bool isGrounded = IsGrounded ();
// Movement input
isGrounded = IsGrounded ();
Vector3 input = new Vector3 (Input.GetAxisRaw ("Horizontal"), 0, Input.GetAxisRaw ("Vertical"));
bool running = Input.GetKey (KeyCode.LeftShift);
targetVelocity = transform.TransformDirection (input.normalized) * ((running) ? runSpeed : walkSpeed);
smoothVelocity = Vector3.SmoothDamp (smoothVelocity, targetVelocity, ref smoothVRef, (isGrounded) ? vSmoothTime : airSmoothTime);
}

void HandleMovement () {
if (Time.timeScale == 0) {
return;
}

if (!debug_playerFrozen && Time.timeScale > 0) {
cam.transform.localEulerAngles = Vector3.right * smoothPitch;
transform.Rotate (Vector3.up * Mathf.DeltaAngle (smoothYawOld, smoothYaw), Space.Self);
}

//bool inWater = referenceBody
if (isGrounded) {
Expand All @@ -128,17 +149,6 @@ void HandleMovement () {
} else {
usingJetpack = false;
}

// Refuel jetpack
if (Time.time - lastJetpackUseTime > jetpackRefuelDelay) {
jetpackFuelPercent = Mathf.Clamp01 (jetpackFuelPercent + Time.deltaTime / jetpackRefuelTime);
}

// Handle animations
float currentSpeed = smoothVelocity.magnitude;
float animationSpeedPercent = (currentSpeed <= walkSpeed) ? currentSpeed / walkSpeed / 2 : currentSpeed / runSpeed;
animator.SetBool ("Grounded", isGrounded);
animator.SetFloat ("Speed", animationSpeedPercent);
}

bool IsGrounded () {
Expand All @@ -165,6 +175,8 @@ bool IsGrounded () {
}

void FixedUpdate () {
HandleMovement();

CelestialBody[] bodies = NBodySimulation.Bodies;
Vector3 gravityOfNearestBody = Vector3.zero;
float nearestSurfaceDst = float.MaxValue;
Expand All @@ -188,7 +200,7 @@ void FixedUpdate () {

// Rotate to align with gravity up
Vector3 gravityUp = -gravityOfNearestBody.normalized;
rb.rotation = Quaternion.FromToRotation (transform.up, gravityUp) * rb.rotation;
transform.rotation = Quaternion.FromToRotation (transform.up, gravityUp) * transform.rotation;

// Move
rb.MovePosition (rb.position + smoothVelocity * Time.fixedDeltaTime);
Expand Down
13 changes: 7 additions & 6 deletions Packages/manifest.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"dependencies": {
"com.unity.collab-proxy": "1.2.16",
"com.unity.ide.rider": "1.1.4",
"com.unity.ide.vscode": "1.2.1",
"com.unity.test-framework": "1.1.14",
"com.unity.textmeshpro": "2.0.1",
"com.unity.timeline": "1.2.14",
"com.unity.collab-proxy": "1.3.9",
"com.unity.ide.rider": "2.0.7",
"com.unity.ide.visualstudio": "2.0.7",
"com.unity.ide.vscode": "1.2.3",
"com.unity.test-framework": "1.1.22",
"com.unity.textmeshpro": "3.0.1",
"com.unity.timeline": "1.4.6",
"com.unity.ugui": "1.0.0",
"com.unity.modules.ai": "1.0.0",
"com.unity.modules.androidjni": "1.0.0",
Expand Down
47 changes: 37 additions & 10 deletions Packages/packages-lock.json
Original file line number Diff line number Diff line change
@@ -1,48 +1,57 @@
{
"dependencies": {
"com.unity.collab-proxy": {
"version": "1.2.16",
"version": "1.3.9",
"depth": 0,
"source": "registry",
"dependencies": {},
"url": "https://packages.unity.com"
},
"com.unity.ext.nunit": {
"version": "1.0.0",
"version": "1.0.6",
"depth": 1,
"source": "registry",
"dependencies": {},
"url": "https://packages.unity.com"
},
"com.unity.ide.rider": {
"version": "1.1.4",
"version": "2.0.7",
"depth": 0,
"source": "registry",
"dependencies": {
"com.unity.test-framework": "1.1.1"
},
"url": "https://packages.unity.com"
},
"com.unity.ide.visualstudio": {
"version": "2.0.7",
"depth": 0,
"source": "registry",
"dependencies": {
"com.unity.test-framework": "1.1.9"
},
"url": "https://packages.unity.com"
},
"com.unity.ide.vscode": {
"version": "1.2.1",
"version": "1.2.3",
"depth": 0,
"source": "registry",
"dependencies": {},
"url": "https://packages.unity.com"
},
"com.unity.test-framework": {
"version": "1.1.14",
"version": "1.1.22",
"depth": 0,
"source": "registry",
"dependencies": {
"com.unity.ext.nunit": "1.0.0",
"com.unity.ext.nunit": "1.0.6",
"com.unity.modules.imgui": "1.0.0",
"com.unity.modules.jsonserialize": "1.0.0"
},
"url": "https://packages.unity.com"
},
"com.unity.textmeshpro": {
"version": "2.0.1",
"version": "3.0.1",
"depth": 0,
"source": "registry",
"dependencies": {
Expand All @@ -51,18 +60,24 @@
"url": "https://packages.unity.com"
},
"com.unity.timeline": {
"version": "1.2.14",
"version": "1.4.6",
"depth": 0,
"source": "registry",
"dependencies": {},
"dependencies": {
"com.unity.modules.director": "1.0.0",
"com.unity.modules.animation": "1.0.0",
"com.unity.modules.audio": "1.0.0",
"com.unity.modules.particlesystem": "1.0.0"
},
"url": "https://packages.unity.com"
},
"com.unity.ugui": {
"version": "1.0.0",
"depth": 0,
"source": "builtin",
"dependencies": {
"com.unity.modules.ui": "1.0.0"
"com.unity.modules.ui": "1.0.0",
"com.unity.modules.imgui": "1.0.0"
}
},
"com.unity.modules.ai": {
Expand Down Expand Up @@ -198,6 +213,18 @@
"depth": 0,
"source": "builtin",
"dependencies": {
"com.unity.modules.ui": "1.0.0",
"com.unity.modules.imgui": "1.0.0",
"com.unity.modules.jsonserialize": "1.0.0",
"com.unity.modules.uielementsnative": "1.0.0"
}
},
"com.unity.modules.uielementsnative": {
"version": "1.0.0",
"depth": 1,
"source": "builtin",
"dependencies": {
"com.unity.modules.ui": "1.0.0",
"com.unity.modules.imgui": "1.0.0",
"com.unity.modules.jsonserialize": "1.0.0"
}
Expand Down
43 changes: 43 additions & 0 deletions ProjectSettings/PackageManagerSettings.asset
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &1
MonoBehaviour:
m_ObjectHideFlags: 61
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 13964, guid: 0000000000000000e000000000000000, type: 0}
m_Name:
m_EditorClassIdentifier:
m_EnablePreviewPackages: 0
m_EnablePackageDependencies: 0
m_AdvancedSettingsExpanded: 1
m_ScopedRegistriesSettingsExpanded: 1
oneTimeWarningShown: 0
m_Registries:
- m_Id: main
m_Name:
m_Url: https://packages.unity.com
m_Scopes: []
m_IsDefault: 1
m_Capabilities: 7
m_UserSelectedRegistryName:
m_UserAddingNewScopedRegistry: 0
m_RegistryInfoDraft:
m_ErrorMessage:
m_Original:
m_Id:
m_Name:
m_Url:
m_Scopes: []
m_IsDefault: 0
m_Capabilities: 0
m_Modified: 0
m_Name:
m_Url:
m_Scopes:
-
m_SelectedScopeIndex: 0
4 changes: 2 additions & 2 deletions ProjectSettings/ProjectVersion.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
m_EditorVersion: 2019.4.1f1
m_EditorVersionWithRevision: 2019.4.1f1 (e6c045e14e4e)
m_EditorVersion: 2020.3.1f1
m_EditorVersionWithRevision: 2020.3.1f1 (77a89f25062f)
8 changes: 8 additions & 0 deletions ProjectSettings/VersionControlSettings.asset
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!890905787 &1
VersionControlSettings:
m_ObjectHideFlags: 0
m_Mode: Visible Meta Files
m_CollabEditorSettings:
inProgressEnabled: 1
24 changes: 24 additions & 0 deletions UserSettings/EditorUserSettings.asset
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!162 &1
EditorUserSettings:
m_ObjectHideFlags: 0
serializedVersion: 4
m_ConfigSettings:
RecentlyUsedScenePath-0:
value: 22424703114646680e0b0227036c6c1f1a160a6a1f31352428245326ece92021
flags: 0
vcSharedLogLevel:
value: 0d5e400f0650
flags: 0
m_VCAutomaticAdd: 1
m_VCDebugCom: 0
m_VCDebugCmd: 0
m_VCDebugOut: 0
m_SemanticMergeMode: 2
m_VCShowFailedCheckout: 1
m_VCOverwriteFailedCheckoutAssets: 1
m_VCProjectOverlayIcons: 1
m_VCHierarchyOverlayIcons: 1
m_VCOtherOverlayIcons: 1
m_VCAllowAsyncUpdate: 1

0 comments on commit a265caa

Please sign in to comment.